Types conforming to multiple protocols in swift

前端 未结 5 536
一整个雨季
一整个雨季 2020-12-13 11:43

I have an Objective-C variable that conforms to multiple protocols.

id  identityToken; 

How would I re

5条回答
  •  醉梦人生
    2020-12-13 12:19

    This should work:

    var identityToken: NSObjectProtocol & NSCopying & NSCoding 
    

    Note you have to use NSObjectProtocol instead of NSObject in swift.

    Here are some additional examples:

    Array of objects conforming to multiple protocols:

    var array: [NSObjectProtocol & NSCopying & NSCoding]
    

    Function with a parameter that conforms to multiple protocols:

    func foo(param: NSObjectProtocol & NSCopying & NSCoding) {
    
    }
    

    For Swift version before 3.1, use:

    var identityToken: (NSObjectProtocol, NSCopying, NSCoding)
    

提交回复
热议问题