Swift option错误Method cannot be a member of an @objc protocol because the type of the parameter 2 cannot be represented in Objective-C解决方法

在swift中protocol如果要用optional的话必须在protocol前加@objc,比如说

enum HomeDataRequestStatus {
    case Normal
    case NoNetWork
    case TimeOut
}

@objc protocol HomeDataManagerDelegate:NSObjectProtocol {
    optional func responseforHomeDynamicDataRequest(response:NSArray?,status:HomeDataRequestStatus)
    optional func pageNoforMoreDynamicData() -> Int
}```

但是responseforHomeDynamicDataRequest这个方法会报错

Method cannot be a member of an @objc protocol because the type of the parameter 2 cannot be represented in Objective-C“`
是因为该方法中的枚举HomeDataRequestStatus没有@objc,在HomeDataRequestStatus前加上@objc,并加上raw type:Int就可以了

@objc enum HomeDataRequestStatus:Int {
    case Normal
    case NoNetWork
    case TimeOut
}

@objc protocol HomeDataManagerDelegate:NSObjectProtocol {
    optional func responseforHomeDynamicDataRequest(response:NSArray?,status:HomeDataRequestStatus)
    optional func pageNoforMoreDynamicData() -> Int
}```
    原文作者:FrYng
    原文地址: https://www.jianshu.com/p/32b5c01ab4f8
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞