Home > Articles > Home & Office Computing > Mac OS X

Foundation: The Objective-C Standard Library

  • Print
  • + Share This
David Chisnall goes through the most important aspects of the OpenStep Foundation framework., which covers the core functionality of the Cocoa development environment and even provides a number of features that would typically be thought of as part of the language, such as reference counting and message forwarding.

Don't miss David Chisnall's live webcast on Cocoa Programming, brought to you by Safari Books Online.
Thursday, March 18
1pm EST / 10am PST

Learn More

This chapter is from the book

The "core" Objective-C language only defines two classes: Object and Protocol. It is rare to use Objective-C without an implementation of OpenStep Foundation, whether it's GNUstep, Cocoa, libfoundation, or Cocotron. The Portable Object Compiler provides its own set of core objects, but it is not widely used.

The OpenStep Foundation is the closest thing that Objective-C has to a standard library, the equivalent of the C standard library or C++'s STL. Of course since Objective-C is a pure superset of C, the C standard library can also be used. The original idea was to do exactly this, and use Objective-C for building components from C software.

Foundation was only introduced with OpenStep to hide the differences between NeXTSTEP's Mach-based operating system and Solaris, and to make it easier to write endian-independent code. Most of Foundation is endian-independent, which was a huge benefit when Apple moved from the big-endian PowerPC to the little-endian x86 architecture.

4.1 General Concepts

Although the Foundation framework is very large, it is quite easy to learn. A lot of the classes share common design principles. When you understand these shared concepts, you can learn how to use each of the individual classes quickly.

4.1.1 Mutability

Objective-C does not have a concept of constant objects. This is not quite true; the const keyword from C still exists, but it only applies to direct access to instance variables. Methods cannot be marked as mutators and so any messages sent to an object may modify it, irrespective of whether the object pointer is const-qualified.

In many cases, however, it is useful to have mutable and immutable versions of objects. This is often done in object-oriented systems by having mutable and immutable classes. Strings are a common example. If you create an Objective-C string literal @ "like_this" then you are creating a constant string. The compiler will put this string in the constants section of the binary—attempting to modify it will cause a segmentation fault. Having to create a new string and copy can make a program very slow, however. This is one of the reasons Java code has a reputation for being slow; Java's String class is immutable, and since it is declared final you can't use Cocoa's solution to the problem, a mutable subclass.

The NSString object is an immutable string. It has a subclass, NSMutableString. Because the mutable version is a subclass, it can be used anywhere that the immutable version can. It implements all of the same methods.

The distinction between mutable and immutable objects is most apparent in the implementation of the -copy method. When you send a -copy message to an immutable object, you often get the same object back (but with the retain count incremented). Because you cannot modify either "copy" they can never become different from each other.

This ability is one of the reasons why Objective-C programs are often faster than C++, in spite of microbenchmarks showing the opposite. In a C++ program, the equivalent with std::string objects would result in a real copy. A C++ string might be copied half a dozen times, whereas a Cocoa string will only have its reference count incremented and decremented.

4.1.2 Class Clusters

Although NSString is the class for immutable strings, your string literal will not really be an NSString. Instead, it will be an NSConstantString or similar. This class is a private subclass of NSString, used for a specific purpose.

This is very common in Cocoa. There might be half a dozen or so different implementations of common classes, such as NSDictionary, all optimized for different uses. When you initialize one, you will get back a specific subclass, rather than the abstract superclass.

There are two ways in which this can be done. The first is to return a different subclass from each constructor or initializer. The second is to use the same instance variable layout and use a trick known as isa-swizzling. The isa pointer, the pointer to the object's class, is just another instance variable. In keeping with the "no magic" philosophy of Objective-C, there is nothing special about it. You can assign a new value to it if you wish. As long as both the new and old classes have the same layout in memory, everything will keep working. (If they don't, you will get some difficult-to-debug memory corruption.)

Class clusters make subclassing slightly difficult. Typically, each of the hidden classes in a cluster implements only a small number of primitive methods. In NSString these are -characterAtIndex: and -length. All of the others are implemented in the superclass in terms of these. If you want to create a new NSString subclass, you must implement these methods yourself. It is common to do this by having a concrete instance as an instance variable and delegating to it, although you can implement the primitive methods yourself.

Of course, there is nothing stopping you from implementing more than just these two primitive methods. You may be able to implement more efficient versions of some of them.

You can implement class clusters of your own very easily. Typically, you will have a set of different initializers in the public class, and each of these will return an instance of a different subclass. To demonstrate this, we will define a simple class encapsulating a pair of values. Listing 4.1 shows this interface. Note that no instance variables are declared here.

In the implementation file, we define two concrete subclasses of the Pair class, one for storing integers and one for floating point values. These are shown in Listing 4.2. Neither of these defines any new methods. Since these interfaces are private, there would be no point in adding new methods since no one would know to call them. They do, however, define the structure. Class clusters implemented like this allow entirely different data layouts for different implementations.

The implementation of the public class, shown in Listing 4.3, is very simple. Most of the methods just return simple default values, since they should not be called. A more robust implementation might throw an exception.

Listing 4.1. The public interface to the pair class. [from: examples/ClassCluster/Pair.h]

 3| @interface Pair : NSObject {}
 4| - (Pair*) initWithFloat:(float)a float:(float)b;
 5| - (Pair*) initWithInt:(int)a int:(int)b;
 6| - (float) firstFloat;
 7| - (float) secondFloat;
 8| - (int) firstInt;
 9| - (int) secondInt;
10| @end

Listing 4.2. The private interfaces to the concrete pair classes. [from: examples/Class-Cluster/Pair.m]

 3| @interface IntPair : Pair {
 4|     int first;
 5|     int second;
 6| }
 7| @end
 8| @interface FloatPair : Pair {
 9|     float first;
10|     float second;
11| }
12| @end

Listing 4.3. The implementation of the public pair class. [from: examples/ClassCluster/-Pair.m]

14| @implementation Pair
15| - (Pair*) initWithFloat: (float)a float: (float)b
16| {
17|     [self release];
18|     return [[FloatPair alloc] initWithFloat: a float: b];
19| }
20| - (Pair*) initWithInt: (int)a int: (int)b
21| {
22|     [self release];
23|     return [[IntPair alloc] initWithInt: a int: b];
24| }
25| - (float) firstFloat { return 0; }
26| - (float) secondFloat { return 0; }
27| - (int) firstInt { return 0; }
28| - (int) secondInt { return 0; }
29| @end

The important thing to note is the [self release] line in both initializers. Typically, an object will be created by first sending +alloc to the Pair class and then sending the result the initialization message. The object returned from +alloc is not required, and so is released here and a new object returned instead.

Listing 4.4 shows the implementations of the private pair classes. Each of these only implements a single constructor, the one relevant to its data type. The accessor methods then either return instance variables or casts of instance variables, allowing both kinds of pair to return ints or floats. One method from NSObject is implemented by both, -description, which provides a human-readable description of the object. Note that neither of these call the designated initializer in the superclass; this is quite bad style, but was done to simplify the example.

Listing 4.4. The implementation of the private pair classes. [from: examples/ClassCluster/Pair.m]

31| @implementation IntPair
32| - (Pair*) initWithInt: (int)a int: (int)b
33| {
34|     first = a;
35|     second = b;
36|     return self;
37| }
38| - (NSString*) description
39| {
40|     return [NSString stringWithFormat: @"(%d,_%d)",
41|            first, second];
42| }
43| - (float) firstFloat { return (float)first; }
44| - (float) secondFloat { return (float)second; }
45| - (int) firstInt { return first; }
46| - (int) secondInt { return second; }
47| @end
48| @implementation FloatPair
49| - (Pair*) initWithFloat: (float)a float: (float)b
50| {
51|     first = a;
52|     second = b;
53|     return self;
54| }
55| - (NSString*) description
56| {
57|     return [NSString stringWithFormat: @"(%f,_%f)",
58|            (double)first, (double)second];
59| }
60| - (float) firstFloat { return first; }
61| - (float) secondFloat { return second; }
62| - (int) firstInt { return (int)first; }
63| - (int) secondInt { return (int)second; }
64| @end

Users of the pair class now don't have to be aware of either of the private classes. A simple test program that creates one of each can demonstrate this. Listing 4.5 shows a short program that just creates two pair objects and logs them. The format string provided to NSLog will cause the -description method in each to be called.

Listing 4.5. Demonstrating the pair classes. [from: examples/ClassCluster/test.m]

 1| #import "Pair.h"
 3| int main(void)
 4| {
 5|     [NSAutoreleasePool new];
 6|     Pair *floats = [[Pair alloc] initWithFloat:0.5 float:12.42];
 7|     Pair *ints= [[Pair alloc] initWithInt:1984 int:2001];
 8|     NSLog(@"Two_floats:_%@", floats);
 9|     NSLog(@"Two_ints:_%@", ints);
10|     return 0;
11| }

Running this program gives the following output:

2009-01-14 14:27:55.091 a.out[80326:10b] Two floats: (0.500000, 12.420000)
2009-01-14 14:27:55.093 a.out[80326:10b] Two ints: (1984, 2001)

A more full implementation of this cluster would have named constructors, such as +pairWithInt:int:, which would avoid the need to allocate and then free an instance of the Pair object. The alternate way of avoiding this, as mentioned earlier, is to use isa-swizzling. The Pair class might have two instance variables that were unions of an int and a float. Implemented in this way, the initializers would look like this:

- (Pair*) initWithFloat: (float)a float: (float)b
    isa = [FloatPair class];
    return [self initWithFloat: a float: b];

This first line in this implementation sets the class pointer to the subclass, and the second calls the method again. Because the class pointer has changed, the second call will invoke the subclass implementation of this method. Each subclass would then refer to the correct field in the union.

  • + Share This
  • 🔖 Save To Your Account