简体   繁体   中英

set NSArray protocol in swift

In Objective-C you can do something like NSArray<ReviewC>* reviews; in order to make a variable NSArray which follows the protocol ReviewC, I need to do the same in Swift, I need to make NSArray follow a protocol, it has to be An NSArray, not an Array.

I know I can do that on Objective C, and that I can mix Objective C with Swift, but I prefer to use Swift if possible.

I think you can do it this way:

protocol ReviewC {
    // protocol definition goes here
}


var reviews : NSArray = NSArray() as? protocol<ReviewC> as! NSArray

You can declare a variable that conforms to a protocol, then assign the NSArray to it, just make sure NSArray conforms to that protocol.

var object: MyProtocol = NSArray(array: someArray)

in your case:

var object: ReviewC = NSArray(array: someArray)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM