简体   繁体   中英

How can I specify protocol same name to interface defined in Objective-C from Swift

When defined as below in Objective-C code, how can I specify Item protocol from Swift?

// Objective-C
@protocol Item <NSObject>
@end

@interface Item : NSObject<Item>
@end

@implementation Item
@end



// Swift
var item = Item() // item interface, but I'd like to define as Item protocol.

Should I define as distinct name?

You cannot instantiate a protocol type , therefore Item() will always refer to @interface Item . You should explicitly specify that you mean a protocol in your declaration:

var item: protocol<Item>

By the way, the thing you've done in Objective-C cannot be done in Swift – it requires all declarations in the same scope to be uniquely named. Your equivalent, following Apple's conventions, would look like this:

@objc(Item) protocol ItemType {

}

class Item: ItemType {

}

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