在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
}```
网友评论