3

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?

shreyasva
  • 13,126
  • 25
  • 78
  • 101

2 Answers2

3

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.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
2

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.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523