We know that in Objective-C there are two main root classes: NSObject and NSProxy. There are other roots (mainly for private and legacy purposes) like Object, and NSLeafProxy.
Defining a new root is fairly trivial:
@interface DDRoot <NSObject>
@end
@implementation DDRoot
//implement the methods required by <NSObject>
@end
My question is: why would you ever want to define a new root class? Is there some use-case where it’s necessary?
As far as I can tell, there should be no reason for creating your own root class, because short of implementing all of the
NSObjectprotocol methods yourself, you’re going to be missing out on a lot of functionality, and going to be making a lot of calls to the Objective-C runtime that should essentially be done for you.Unless you really had to implement the protocol differently from the default (
NSProxyis a special case that does have to), you shouldn’t need to make your own root class. I mean, you’d have to be writing a class that cannot fundamentally be represented byNSObjectand the protocol as implemented by Apple, and in that case, why are you even writing it in Objective-C?That’s what I think. Maybe someone can come up for a creative use for it.
(People researching the topic should go look at the NSObject Class Reference, NSObject Protocol Reference, ‘Core Competencies: Root Class’ document, and the ‘Root Class’ section of the Fundamentals Guide: Cocoa Objects document.)