I’d like to write an Objective-C class without Cocoa or GNU’s Object.h (for educational purposes). I dug around the net and it seems to me that quite a lot of stuff that one would expect to “come with the language”, such as classes and message sending are actually defined in files written by third parties, such as objc-runtime.h.
Is there any documentation about what is really pure Objective-C and what is part of the runtime / frameworks? And what functionality do I have to implement to get a working environment without using any third-party code such as Object.h or objc-runtime.h (note again that this is for educational purposes, not for production code)?
Thanks for any insight!
Really, the only thing you must take care of yourself if you don’t inherit from
NSObjectis object creation and destruction; methods otherwise behave the same way regardless of their parent class. Features like KVC and memory management are features of OpenStep/Cocoa, but not required as part of the language.Here’s a class from scratch:
And here’s how it could be used:
Edit: If you don’t even want to use
class_createInstance()andobject_dispose(), you’ll have to implement equivalents manually, as well as an equivalent ofclass_getInstanceSize()so you know how much memory an object occupies. But even if you manage that, don’t think you’ve escaped the Objective-C runtime! Message dispatch is still entirely built on the C functions in the runtime, and Objective-C syntax is transformed into calls to those functions during compilation.