Why subclass NSObject?

前端 未结 5 1083
梦谈多话
梦谈多话 2020-12-10 01:46

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

@interface Fraction : NSObject 

In C++ or

相关标签:
5条回答
  • 2020-12-10 02:03

    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);
    }
    
    0 讨论(0)
  • 2020-12-10 02:11

    NSObject

    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.

    From Apple documentation - https://developer.apple.com/documentation/objectivec/nsobject.

    Basically, most of OOP programming languages explicitly or implicitly specify base class or base functionality. Otherwise you cannot build system where objects communicate with each other. Properties, memory management, message sending mechanism are partly or completely provided or supported by NSObject. Apple provide parts of the Objective-C implementation - https://opensource.apple.com/source/objc4/objc4-723/runtime/NSObject.mm.auto.html, where it's possible to see what is actually inside NSObject.

    Also because Objective-C is a language from C-family, so compiler and linker needs to calculate how to layout object in memory and where put and find methods, that's only possible if you know how each of the classes/instances lays in memory and where. In case of Objective-C all base classes (NSObject, NSProxy, etc) have specification of that, so it's possible to calculate their size and add on top all inherited stuff - https://clang.llvm.org/compatibility.html#objective-c.

    Consequently compiler don't let to leave a class without base class. So in the end class inheritance should lead to one of the root classes. Here is the error that appears if you don't specify it (from Xcode):

    Class 'ClassWithoutBaseClass' defined without specifying a base class

    0 讨论(0)
  • 2020-12-10 02:12

    All classes don't necessarily inherit from NSObject but it is the core for many of the classes because it provides things like alloc, retain, and release.

    0 讨论(0)
  • 2020-12-10 02:13

    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.

    0 讨论(0)
  • 2020-12-10 02:15

    NSObject is the root class of all classes. In my estimation, it's 3 most basic functions are to allocate and initialize memory for you (alloc & init), as well as provide a description function.

    Objective-C is all about objects sending messages to other objects -- so NSObject exists to provide that basic functionality.

    If this sounds strange to you, you may wish to read more about programming paradigms, particularly object-oriented programming....In a nutshell, however, Objective C is a simple extension to the C language. C gets you the ability to program computer memory, numbers, and characters, but do anything else (like use strings, or show views, for example) you need the extension part, and NSObject is the beginning of that extension.

    It may be a useful exercise to pick a class (like NSString, or any for that matter), and follow it's superclasses back to NSObject, to see what functionality each class added.

    Hope that helps...

    0 讨论(0)
提交回复
热议问题