BlackMamba
BlackMamba

Reputation: 10252

About Delegate in objective-c?

@protocol MyButtonViewDelegate <NSObject>
- (void)buttonView:(MyButtonView*)view buttonPressed:(UIButton*)button;
@end

i am new in objective-c, i am learning delegate according the book.

from the book, when we define protocol we just write like this @protocol MyButtonViewDelegate.

But what's the difference beteween @protocol MyButtonViewDelegate and @protocol MyButtonViewDelegate <NSObject>. Why we need <NSObject>?

Upvotes: 1

Views: 80

Answers (1)

Caleb
Caleb

Reputation: 125017

But what's the difference beteween @protocol MyButtonViewDelegate and @protocol MyButtonViewDelegate <NSObject>.

The <NSObject> says that the protocol MyButtonViewDelegate conforms to the NSObject protocol. That is, any object that conforms to the MyButtonViewDelegate protocol must also conform to the NSObject protocol. (You may not have realized it, but there's a protocol named NSObject as well as a class by that same name.) So, if you've got an object that conforms to MyButtonViewDelegate, it's safe to call methods like -hash, -isEqual:, -retain, -release, -isKindOfClass:, etc.

Every object you're likely to encounter will already conform to NSObject because the class NSObject conforms to protocol NSObject. The only other Objective-C base class that you might run into is NSProxy, and that also conforms to NSObject. Therefore, adding <NSObject> to your protocol probably won't make a real difference, but it's a nice way to make the requirement explicit.

Upvotes: 6

Related Questions