神刀安全网

NSObject (1994)

NSObject is the root class of all ordinary Objective C inheritance hierarchies; it’s the one class that has no superclass. From NSObject, other classes inherit a basic interface to the run-time system for the Objective C language.  It’s through NSObject that instances of all classes obtain their ability to behave as objects.

Among other things, the NSObject class provides inheriting classes with a framework for creating, initializing, deallocating, copying, comparing, and archiving objects, for performing methods selected at run-time, for querying an object about its methods and its position in the inheritance hierarchy, and for forwarding messages to other objects. For example, to ask an object what class it belongs to, you’d send it a class message ( class is declared in the NSObject protocol). To find out whether it implements a particular method, you’d send it a respondsToSelector: message.

The NSObject class is an abstract class; programs use instances of classes that inherit from NSObject, but never of NSObject itself.

Initializing an Object to Its Class

Every object is connected to the run-time system through its isa instance variable, inherited from the NSObject class. isa identifies the object’s class; it points to a structure that’s compiled from the class definition. Through isa , an object can find whatever information it needs at run time–such as its place in the inheritance hierarchy, the size and structure of its instance variables, and the location of the method implementations it can perform in response to messages.

Because all objects directly or indirectly inherit from the NSObject class, they all have this variable.  The defining characteristic of an "object" is that its first instance variable is an isa pointer to a class structure.

The installation of the class structure–the initialization of isa –is one of the responsibilities of the alloc , allocWithZone: , and new methods, the same methods that create (allocate memory for) new instances of a class. In other words, class initialization is part of the process of creating an object; it’s not left to the methods, such as init , that initialize individual objects with their particular characteristics.

Instance and Class Methods

Every object requires an interface to the run-time system, whether it’s an instance object or a class object.  For example, it should be possible to ask either an instance or a class about its position in the inheritance hierarchy or whether it can respond to a particular message.

So that this won’t mean implementing every NSObject method twice, once as an instance method and again as a class method, the run-time system treats methods defined in the root class in a special way:

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » NSObject (1994)

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址