简体   繁体   English

一个类是否可能符合Objective-C中的多个协议?

[英]Is it possible for a class to conform to more than one protocol in objective-c?

Is it possible for a class to conform to more than one protocol in objective-c? 一个类是否可能符合Objective-C中的多个协议? If so, what is the syntax for declaring a class that conforms to more than one protocol? 如果是这样,则声明一个符合多个协议的类的语法是什么?

@interface MyClass : NSObject <Protocol1, Protocol2, Protocol3>

@end

Yes; 是; Just put a comma between each Protocol. 只需在每个协议之间使用逗号即可。

Yes it is possible for a class to conform to multiple protocols. 是的,一个类可能符合多种协议。 The syntax is as follows: 语法如下:

@interface MyClass : NSObject <Protocol1, Protocol2, Protocol3>
//...Some code here...
@end

A protocol in Objective-C is essentially a list of methods which must be implemented in order for an object or class to be said to be conforming to that protocol. Objective-C中的协议本质上是方法的列表,必须执行这些方法才能使对象或类符合该协议。 A common example of a class conforming to multiple protocols is a UITableViewController that acts as a UITableViewDataSource and a UITableViewDelegate. 符合多种协议的类的常见示例是充当UITableViewDataSource和UITableViewDelegate的UITableViewController。

For a UITableViewController example, it might look like this: 对于UITableViewController示例,它可能如下所示:

@interface MyTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
//...Some code here...
@end

You separate each protocol with a comma, and put it inside of those brackets. 您用逗号分隔每个协议,并将其放在这些括号内。 When you add those protocols to your interface declaration, you're essentially saying "yes, I'll implement the methods defined by those protocols". 当您将这些协议添加到接口声明中时,您实际上是在说“是的,我将实现那些协议定义的方法”。 Now, go ahead and implement those methods, or the compiler will remind you that you haven't kept your word. 现在,继续执行这些方法,否则编译器会提醒您您没有遵守诺言。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Objective-C 警告:Class 'ViewController' 不符合协议 'CBPeripheralManagerDelegate' - Objective-C warning: Class 'ViewController' does not conform to protocol 'CBPeripheralManagerDelegate' Swift 类不符合带有错误处理的 Objective-C 协议 - Swift class does not conform to Objective-C protocol with error handling Objective-c 类可以符合 swift 协议吗? - Can an objective-c class conform to a swift protocol? 类不符合NSItemProviderWriting协议-Objective-C - Class does not conform to NSItemProviderWriting protocol - Objective-C 如何使Objective-C类符合Swift中定义的协议? - How to make an Objective-C class conform to a protocol defined in Swift? 如何使 Swift class 符合 Objective-C 协议,而不暴露给 ZA5084C5B36F32512E6AZ? - How to make Swift class conform to Objective-C protocol, without exposing it to Objective-C? 迅速不符合Objective-C协议 - Cannot conform to objective-c protocol in swift Objective-C:将一个实现协议的类别添加到现有类中,是否使对象符合该协议? - Objective-C: Does adding a Category, that implements a Protocol, to an existing Class, make Objects conform to that Protocol? 如何创建符合Swift和Objective-C之间共享协议的类方法? - How to create class methods that conform to a protocol shared between Swift and Objective-C? 如何让Objective-C类符合Swift的`Equatable`协议? - How can I make my Objective-C class conform to Swift's `Equatable` protocol?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM