The Objective-C Language05/04/2001
Last time, we talked about the major concepts of object-oriented programming. Today, I want to tell you how Objective-C implements these concepts in the language, as well as show you what Objective-C code actually looks like.
The most fundamental ability we must have in object-oriented programming is the ability to send messages to objects. To send a message to an object in Objective-C, we use the following construct:
The receiver is the object you are sending the message to, and message is the method you wish the receiving object to perform. For example, suppose we have an object
dataTable in our program whose data we want to update. To do this, we would have the following line of code in our program which is a message to the table:
Arguments in Objective-C methods are indicated by a colon.
You can have any number of arguments, each preceded by a colon. Messages can also be nested within each other, as long as the argument types and return types agree. For example, we could read the value from a slider control and then display it in a text field.
[textField setDoubleValue:[slider doubleValue]];
Here we see that
[slider doubleValue] is the argument of
[textField setDoubleValue: ]. The
doubleValue command returns a type double number, which is the data type of the argument of
Speaking of data types, objects in Objective-C are all given the data type
id. Variables of type
id are nothing more than object identifiers. They are actually just pointers to the object's data structure; this is beyond the scope of our discussion now, however. In code, we would create variables that refer to objects like any other variable.
In Objective-C, the default return value data type is
id. So, if you have a method that has no return value data type, then it will automatically be given type
|Now that we've waded knee-deep into Objective-C programming, how is the column working for you?|
|Post your comments|
It's also possible to type a data variable to a specific class. This is known as static typing. All variables that refer to objects are really pointers to their memory location. This implementation is pretty transparent to the programmer in most places. However, when it comes to static typing, it is not so transparent.
When we create a variable of type
id the fact that it is a pointer to an object is implicit in the typing.
id is by definition a pointer type -- an object identifier. But if you have a variable that refers to a string, and you want to statically type it to NSString, you have to make it explicit in code that the variable is a pointer. This is done by using the C pointer declaration syntax:
The astrisk in front of the variable name is not part of the name, but rather indicates that
aString is a pointer to some
NSString object. This is the only place you need to be aware of the pointer nature of object identifier variables. Only when declaring new variables do you need to use the C pointer construct. Whenever you subsequently refer to
aString, it will be as any other variable.
If you're not comfortable with pointers, or C itself check out The C Programming Language by Brian Kerrnighan and Dennis Ritchie (they actually wrote the C language). I also like and recommend Practical C Programming by Steve Oulline.
Options for creating nested methods
Now that we have a data type for objects, we can create nested methods in another way than above, where a message with return value type
id can be the receiver of another message.
double number; number = [[anArray lastObject] doubleValue];
Of course we're assuming here that the object returned by
[anArray lastObject] does indeed respond to the
doubleValue message we send to it above. What is happening is that we have an array of objects. The message
[anArray lastObject] returns the object in the array with the highest index, which is then sent a
doubleValue message. Alternatively, we can code this in a more verbose fashion:
id tempObject; double number; tempObject = [anArray lastObject]; number = [tempObject doubleValue]; Defining Classes