Monkeyanator
Monkeyanator

Reputation: 1416

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?

Upvotes: 0

Views: 297

Answers (3)

Moshe
Moshe

Reputation: 58087

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.

Upvotes: 3

Psycho
Psycho

Reputation: 1883

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

@end

Upvotes: 7

w.donahue
w.donahue

Reputation: 10886

Yes; Just put a comma between each Protocol.

Upvotes: 3

Related Questions