tags:

views:

75

answers:

1

The documentation says:

While not strictly a part of the language, the isa pointer is required for an object to work with the Objective-C runtime system. An object needs to be “equivalent” to a struct objc_object (defined in objc/objc.h) in whatever fields the structure defines. However, you rarely, if ever, need to create your own root object, and objects that inherit from NSObject or NSProxy automatically have the isa variable.

While that sounds nice, I wonder how an root object would be created in Objective-C anyways?

This is for learning purposes. I just want to know this. I'd really like to see it.

+5  A: 

It's actually a "trap" some people migrating from C# or Java style languages fall into. You simply don't specify a superclass when declaring your class i.e.

@interface MyNewRoot {
Class isa;
}
@end

vs

@interface MyObject : NSObject {
}
@end

In Java or C# these would be equivalent (in the first case the compiler would assume System.Object or java.lang.Object was the superclass), but in Objective-C no such default will be assumed, and hence a new root is created.

However you're now responsible for a number of features for your class that you typically take for granted (even simple things like memory management for allocating or destorying new instances etc). This is what the comment you quoted hints at when it talks about struct objc_object and the isa instance variable etc.

Christopher Fairbairn
Added the isa pointer to your definition of MyNewRoot. You can't have an object with no ivars at all, you've got to have the pointer to the class at least.
NSResponder