Journal Articles
Browse in : |
All
> Journals
> CVu
> 165
(13)
All > Topics > Programming (877) Any of these categories - All of these categories |
Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.
Title: An Introduction to Objective-C
Author: Administrator
Date: 03 October 2004 13:16:07 +01:00 or Sun, 03 October 2004 13:16:07 +01:00
Summary:
Body:
Unlike C++, Objective-C is standard C with a small object-oriented extension; an Objective-C compiler will compile all conforming C code in exactly the same way as a C compiler.
Objective-C source files traditionally have the suffix .m; and with the Apple/NeXT compiler, a file that contains a mixture of Objective-C and C++ code (so-called Objective C++) needs to be suffixed with .mm.
Objective-C adds one type to the C language, namely id, which is a pointer to any object.
id myObject;
declares an object pointer with the name myObject.
If the class of the object is known, the above declaration is equivalent to:
MyClass *myObject;
which declares a pointer to an object of class MyClass. This syntax allows the compiler to perform static type-checking, so that the programmer can be informed at compile time that an object of that class does not support a particular operation.
In Objective-C, unlike C++, all objects are referenced by means of pointers.
Classes are declared as follows:
#import "BaseClass.h" @interface MyClass : BaseClass { // Instance variables int n; float x; } // Method declarations // Class method + (id)new; // Instance method with arguments - (id)initWithValues:(int)nn and:(float)xx; @end
This is the public interface of the class MyClass, which inherits from BaseClass.
By default, instance variables have 'protected' visibility; this means that they can be read and written to directly only by objects of the class in which they are declared or of a class inheriting from it. The default visibility can be varied by means of the compiler directives @public and @private; public variables are visible to all outside code, while private ones are accessible only to instances of the class in which they are declared.
A class method is prefixed by + and could be called in the following manner to create an instance of the class and its address stored in a variable:
MyClass *newObject = [MyClass new];
Every class is an object in Objective-C and typically contains methods, like new above, to create instances of itself.
The return type of a method and the type of its arguments are written within parentheses. If the type is not provided, type id is assumed. It is important to understand that type names within parentheses resemble the C syntax for 'casting' a value from one type to another, in this context no casting is involved.
Instance methods belong to an instance of the class and can access its instance variables. Their declarations are preceded by the token -.
Arguments are declared after keywords terminating in colons; the name of the method is understood to consist of all the keywords together with the colons. The instance method above has the name initWithValues:and:, and it is pronounced: 'init with values colon and colon'. The method could be invoked in the following manner:
[newObject initWithValues:42 and:12.576];
This style of naming methods will initially seem strange to those who are unfamiliar with Smalltalk, from which it is derived.
The implementation of a class is defined as follows:
#import "MyClass.h" @implementation Myclass + (id)new { // Allocate memory for an object and // initialise to default values. return [[super alloc] init]; } // -init is a private method and is thus not // declared in the interface. - (id)init { // Initialise self to default values. return [self initWithValues:0 and:0.0]; } - (id)initWithValues:(int)nn and:(float)xx { // Call BaseClass init method. [super init]; // Initialise the instance variables and // return a pointer to this object. n = nn; x = xx; return self; } @end
The keyword self is a pointer to the current object; super is a directive that instructs the compiler to invoke a method in the superclass (i.e. the class from which the current one is derived, otherwise known as 'base class').
The normal way to invoke methods in Objective-C is the write the object name (known as the receiver) followed by the method name (with arguments, if required) between square brackets; the whole is known as a message expression.
The method name is called a 'selector' because the receiving object uses it to select the appropriate method for the required operation. The message expression:
[self initWithValues:0 and:0.0]
above could be described in English as: 'send self a message with selector initWithValues and parameters 0 and 0.0'.
The reader should now have enough information to know how to write Objective-C classes, how instances are created and how to invoke methods.
Apart from knowledge of supplied class libraries, little else is required in order to become a competent user of the language.
Notes:
More fields may be available via dynamicdata ..