I began a course for Apple application development which requires me to learn Objective-C. I decided a good way to learn the language would be to write short blogs on the subject. So to begin, some notes about Objective-C…
Objective-C allows use of C as well as C++ comments // and /**/
Based on C
The language is based on C, so we have access to types such as int, float, char, etc.
Many new keywords have been added. Some of which are used to directly support object oriented programming. These new keywords are prefixed with ‘@’ to avoid naming conflicts: @class, @interface, @end, @implementation, @protocol are some examples.
The language is header based. We have a (.h) header file and (.m) implementation file.
The .h file contains our ‘C++ class declaration’ which we call the interface. It is in this file which we have the interface, properties and methods.
The .m (which stands for module) contains the implementation for the interface. We write the definitions for the methods and synthesize our properties.
We can use C-style ASCII “strings” as well as Objective-C @”strings” which can support ASCII and Unicode.
Objective-C has a preprocessor. Instead of using #include we use #import. The import directive does not need to be used in conjunction with include guards.
In Java every object inherits from the Object class which has a toString method. In Objective-C, objects which inherit from NSObject can be sent a description message for equivalent behaviour.
Like C#, Objective-C has direct support for properties. Unlike
C#, we can make Objective-C generate accessor functions.
In C++ we use new to dynamically allocate memory for an object. We can then create many pointers to the same object. When the object is no longer needed, we use delete to free the memory. At that point, no pointer which pointed to that object should be dereferenced.
Objective-C uses a reference counting system. We use keywords retain and release to increase and decrease references to an object once it has been initialized. When an object’s reference counter reaches 0, it is freed by the runtime and a reference pointing to that part of memory should not be sent a message.
Like Java, Objective-C only supports single inheritance. And like Java, it supports interfaces, which we call protocols. Furthermore, unlike C++, we can only publicly inherit from another class.