美文网首页Swifty CodingiOS开发iOS Developer
Swift option错误Method cannot be a

Swift option错误Method cannot be a

作者: 9ad91f89b29e | 来源:发表于2016-08-04 19:23 被阅读2580次

    在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
    }```

    相关文章

      网友评论

        本文标题:Swift option错误Method cannot be a

        本文链接:https://www.haomeiwen.com/subject/cithsttx.html