Objective-C is a thin layer built on top of C, but you do not need to be a C expert in order to use it. In fact, only a relatively small subset of C is used for most Objective-C projects. The reason is that the classes in the Cocoa frameworks implement many of the functions that you might otherwise work with in C. For example, with a few exceptions, handling of strings and numbers in Cocoa is done in the Cocoa classes and not in raw C. This appendix lists some of the key syntax elements that you use with Objective-C.
You can find tutorials about C on the Web and in books such as Sams Teach Yourself C in 21 Days by Bradley L. Jones and Peter Aitkin. The classic reference is The C Programming Language (2nd Edition) by Brian W. Kernighan and Dennis M. Ritchie.
C supports the same basic types as most programming languages today: signed and unsigned char
, short
, bool
, int
, and long
as well as float
and double
. Most of the time, you either use Cocoa classes or Core Foundation references to these types such as NSString
(a class) or CFString
(a reference).
Instead of defining types by characteristics such as integer, you can define a type by its values. They are implicitly numbered from zero, but you can also add specific numbers.
enum days {
monday,
tuesday,
wednesday,
thursday,
friday,
saturday = 101,
sunday = 102
};
These are based on structured records. You can combine primitive types into a single structure that can be manipulated. structs are used in various places in Core Foundation, but if you have a background in C and are used to using structs, ask yourself if you are actually better off creating a class with instance variables or declared properties.
struct bankAccount {
float balance;
float creditLimit;
};
Declare a variable of the struct type:
struct bankAccount myAccount;
Access an internal struct element:
myAccount.balance = 0;
Pointers are used constantly throughout Objective-C. They point to primitive types or to structures (such as objects) as in this line of code:
NSError *error = nil;
Pointers can be dereferenced with & so as to get to the underlying data. This is commonly used with NSError
. Some methods use double indirection in their declaration as shown here for outError
:
– (BOOL)readFromURL:(NSURL *)url error:(NSError **)outError
You can invoke it with code such as
myResult = [myDocument readFromURL:myURL error: &error];
The argument url
is a pointer to the NSURL
class. outError
is a pointer to a pointer to the NSError
class (this is called double indirection). If the method makes a change to URL
, it affects the object passed in via the argument. With the double indirection of outError
, the method can change the pointer so that it points to another object. Typically, it is used to change it from its initial value (nil) to another instance of NSError
. In this way, a method can return a new value not only as its result (BOOL in this case) but also through a doubly indirected argument.
In Objective-C, arrays are frequently declared with the Core Foundation opaque CFArray
reference. More often, arrays are used as one of the collection objects (NSArray
, NSSet
, and NSDictionary
).
As in most other programming languages, control passes from one statement to the next one. Functions or methods can be called; they might return a value that is used in a subsequent statement. In common with other object-oriented languages, long sequences of step-by-step code (“spaghetti code”) are frowned on. Anything that organizes the control structure makes the code more maintainable. Three basic control structures (other than function and method calling) are used extensively.
Here is a typical use of an if
statement.
if (someConditionIsTrue ) {
return (YES);
} else {
return (NO);
}
For many people, omitting the break at the end of each case is a very common error.
switch(type) {
case oneCase:
doSomething ();
break;
case anotherCase:
doSomethingElse ();
break;
}
Look at the fast enumeration and the NSEnumerator
class in Cocoa when you are tempted to use a repeat statement. If you do want to use a C repeat structure, here are the three basic forms:
do
<code>
while ( <condition> ) ;
while ( <condition> )
<code>
for ( <condition> ; <condition> ; <condition> )
<code>
3.143.0.85