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.