Difference between id <protocol> and NSObject <protocol>

In Objective-C, id<protocol> or NSObject<protocol> often used to declare a delegate.

What are the main differences between id and NSObject? When do you want to use one or the other?

+6
source share
1 answer

id<protocol> obj is the declaration for any object that conforms to the specified protocol. You can send any message from this protocol to the object (or from protocols that <protocol> inherited from).

NSObject<protocol> *obj is an ad for any object that

  • complies with this protocol and
  • NSObject from NSObject .

This means that in the second case, you can send any methods from the NSObject class to the object, for example

 id y = [obj copy]; 

which will give a compiler error in the first case.

The second declaration also implies that obj conforms to the NSObject protocol. But it doesn’t matter if the <protocol> obtained from the NSObject protocol:

 @protocol protocol <NSObject> 
+8
source

All Articles