当前位置: 动力学知识库 > 问答 > 编程问答 >

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? 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. A common example of a class conforming to multiple protocols is a UITableViewController that acts as a UITableViewDataSource and a UITableViewDelegate.

For a UITableViewController example, it might look like this:

@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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: