Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why subclass NSObject?

Tags:

What is the purpose/use of NSObject in Objective-C? I see classes that extend NSObject like this:

@interface Fraction : NSObject  

In C++ or Java, we don't use any variables like NSObject even though we have preprocessor directives and import statements in both Objective-C and Java.

Why do classes explicitly inherit from NSObject in Objective-C? What are the consequences of not declaring inheritance from NSObject?

like image 784
user185590 Avatar asked Oct 19 '09 11:10

user185590


People also ask

Why do we inherit NSObject?

This is because Objective-C is all about objects sending messages to other object. NSObject exists to mark a class in Swift that you will provide that basic functionality — basically Objective-C needs you to build up from a base class with your functionality on top (That is, NSObject is a Universal Base Class).

What is the use of NSObject in Swift?

The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.

Do all Swift classes inherit from NSObject?

Swift classes do not inherit from a universal base class. Classes you define without specifying a superclass automatically become base classes for you to build upon.

What is NSObject Swiftui?

NSObject is what's called a universal base class for all Cocoa Touch classes. That means all UIKit classes ultimately come from NSObject , including all of UIKit.


2 Answers

We use NSObject to explicitly state what a given class inherits from. I'm not sure about C++, but in Java there's something similar - the Object class. The only difference is that Java doesn't require that classes explicitly descend from Object - the language assumes anything that doesn't have a specified parent class descends from Object. Objective-C is different because it allows you to define different root classes - you are allowed to make a class that doesn't inherit from NSObject.

An example of such a different root class is NSProxy.

Have a look at the GNUstep NSObject source, it shows how the methods interact with the objective-c runtime through C functions.

+ (id) allocWithZone:(NSZone*)z {   return NSAllocateObject(self, 0, z); }  - (void) dealloc {   NSDeallocateObject (self); }  + (BOOL) isSubclassOfClass: (Class)aClass {   return GSObjCIsKindOf(self, aClass); } 
like image 172
Tim Avatar answered Sep 25 '22 07:09

Tim


Since object-oriented languages have the concept of an inheritance, in any inheritance hierarchy there is a root class. In Java, the default parent class (if none is provided) is java.lang.Object, whereas in Objective-C, if you don't explicitly declare a parent class, you don't get one. Essentially, your class becomes a root class itself. This is a common mistake among Objective-C newcomers, since you normally want to inherit from NSObject in such cases.

While often problematic and puzzling, this actually allows quite a bit of flexibility, since you can define your own class hierarchies that act completely differently from NSObject. (Java doesn't allow you to do this at all.) On the other hand, unless you know what you're doing, it's easy to get yourself into trouble this way. Fortunately, the compiler will provide warnings if you call a method not defined by a class with no declared parent class, such as those you would normally expect to inherit from NSObject.

As for the "use" of NSObject, check out the documentation of the NSObject class and NSObject protocol. They define common methods used for object allocation, memory management, comparison, hashing, printing descriptions, checking class membership, querying whether objects respond to a selector, etc. Basically, NSObject is "good for" providing the core functionality of Objective-C objects free of charge.

like image 42
Quinn Taylor Avatar answered Sep 22 '22 07:09

Quinn Taylor