Is it possible to create a class in Objective-C without a superclass.
If I create something like
@interface Samp
@end
I get the error message: "Class Samp defined without specifying a base class". How come NSProxy compiles?
Is it possible to create a class in Objective-C without a superclass.
If I create something like
@interface Samp
@end
I get the error message: "Class Samp defined without specifying a base class". How come NSProxy compiles?
The pedantic answer is yes, you can. You just have to make your class a root class, which you can do by using the compiler attribute objc_root_class
.
__attribute__((objc_root_class))
@interface Samp
@end
or using the convenience macro NS_ROOT_CLASS
NS_ROOT_CLASS
@interface Samp
@end
This is the same thing NSProxy
does.
NS_ROOT_CLASS
@interface NSProxy <NSObject> {
Class isa;
}
Now, unless you're doing something really specific and out of the schemes, I don't see why you would want to do that.
Juts make your class to inherit from an existing root class, like NSObject
(the most common) or NSProxy
.
How come
NSProxy
compiles?
NSProxy
class is special - it is one of Cocoa's two public root classes (NSObject
is the other one). If you must define your own root class, this answer tells you how to do it. However, in practice there should be no reason to implement your own root class.