`
janedoneway
  • 浏览: 569783 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

确实应该使用NSObject来作为我们类的Base Class。

 
阅读更多

From: http://www.cocoachina.com/bbs/read.php?tid-276.html

 

NSObject is the root class (a root class is a class with no superclass) of the GNUstep base library class hierarchy, so all classes normally inherit from NSObject. There is an exception though: NSProxy (which is used for remote messaging) does not inherit from NSObject.  
 
 
Unless you are really sure of what you are doing, all your own classes should inherit (directly or indirectly) from NSObject (or in special cases from NSProxy). NSObject provides the basic common functionality shared by all GNUstep classes and objects.  
 
 
The essential methods which must be implemented by all classes for their instances to be usable within GNUstep are declared in a separate protocol, which is the NSObject protocol. Both NSObject and NSProxy conform to this protocol, which means all objects in a GNUstep application will conform to this protocol (btw, if you don't find a method of NSObject you are looking for in this documentation, make sure you also look into the documentation for the NSObject protocol).  
 
 
Theoretically, in special cases you might need to implement a new root class. If you do, you need to make sure that your root class conforms (at least) to the NSObject protocol, otherwise it will not interact correctly with the GNUstep framework. Said that, I must note that I have never seen a case in which a new root class is needed.  
 
 
NSObject is a root class, which implies that instance methods of NSObject are treated in a special way by the Objective-C runtime. This is an exception to the normal way messaging works with class and instance methods: if the Objective-C runtime can't find a class method for a class object, as a last resort it looks for an instance method of the root class with the same name, and executes it if it finds it. This means that instance methods of the root class (such as NSObject) can be performed by class objects which inherit from that root class ! This can only happen if the class doesn't have a class method with the same name, otherwise that method - of course - takes the precedence. Because of this exception, NSObject 's instance methods are written in such a way that they work both on NSObject 's instances and on class objects. 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics