In Objective-C, id<protocol> or NSObject<protocol>
are frequently used for delegate declaration.
What are the main differences between id and NSObject? When do you want to use one versus the other?
id is a language keyword but the NSObject is the base class in objective c. For id, you dont need to typecast the object. But for NSObject, you have to typecast it into NSObject.
The group of methods that are fundamental to all Objective-C objects.
Creating a Custom Class Go ahead an choose “Objective-C class” and hit Next. For the class name, enter “Person.” Make it a subclass of NSObject. NSObject is the base class for all objects in Apple's developer environment. It provides basic properties and functions for memory management and allocation.
id<protocol> obj
is the declaration for any object that conforms to the specified protocol.
You can send any message from the given protocol to the object (or from the protocols
that <protocol>
inherits from).
NSObject<protocol> *obj
is the declaration for any object that
NSObject
.That 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 would give a compiler error in the first case.
The second declaration also implies that obj
conforms to the NSObject
protocol.
But this makes no difference if <protocol>
is derived from the NSObject protocol:
@protocol protocol <NSObject>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With