Wednesday, September 16, 2015

Objective C NSObject Root Class

All the Objective C programmers know that the root class of all Objective C classes is NSObject. It is part of NSFoundation framework. Root class inherits from no other class. Let's see why it a root class and what it gives us to maintain that big name called 'Root Class'.

Here I am just detailing the information given in the Apple Documentation. I feel like the information is briefed in that documentation. I Just want to discuss in detail on this topic in this article. So If any one finds it similar to that apple documentation, please don't come back here and call me a copy cat :)

1. NSObject declares the fundamental object interface and 
    implements basic object behaviour.

2. NSObject gives us Introspection methods :

  • Methods like isKindOfClass and isMemberOfClass, to determine if an object inherits directly or indirectly from a particular class. For more information on these methods, checkout out my article here.
  • Methods like respondsToSelector, to find out if an object's class or super class implements a method.
  • Methods like confirmsToProtocol, to find out if a class confirms a particular protocol or not.

      So from this, and also as per the Apple Documentation,

  • You can call introspection methods as runtime checks to help you avoid problems such as exceptions, which, for example, would occur if you send a message to an object that cannot respond to it.
  • You can also use introspection to help locate an object in the inheritance hierarchy, which would give you information about the object’s capabilities.
3. NSObject gives us Memory management methods :

     Methods like alloc, dealloc, retain, copy etc.

4. NSObject gives us Method Invocation methods.

As it is giving us all these, we call NSObject as the root class of all Objective C classes.