Trace: » subversion » sdl » avr_c » algorithms » text » book » c_plus_plus » flash » advoop » cocoa_basics

Table of Contents

Cocoa Basics

@class

@class sets up a forward reference. This is a way to tell the compiler, “Trust me; you’ll learn eventually what this class is, but for now, this is all you need to know.” @class is also useful if you have a circular dependency. That is, class A uses class B, and class B uses class A. If you try having each class #import the other, you’ll end up with compilation errors. But if you use @class B in A.h and @class A in B.h, the two classes can refer to each other happily.

@protocol

A formal protocol (like an informal protocol) is a named list of methods. But unlike an informal protocol, a formal one requires that you explicitly adopt the protocol. You adopt a protocol by listing the protocol’s name in your class’s @interface declaration.

Declaring Protocols

@protocol NSCopying 
- (id) copyWithZone: (NSZone *) zone; 
@end 

The syntax looks kind of the same as the syntax for declaring a class or a category. Rather than using @interface, you use @protocol to tell the compiler, “I’m about to show you what a new formal protocol will look like.” That statement is followed by the protocol name. Protocol names must be unique.

Next is a list of method declarations, which every protocol adopter must implement. The protocol declaration finishes with @end. There are no instance variables introduced with a protocol.

Adopting a Protocol

To adopt a protocol, you list the protocol in the class declaration, surrounded by angle brackets. For example, if Car adopts NSCopying, the declaration looks like this:

@interface Car : NSObject <NSCopying> 
{ 
  // instance variables 
} 
// methods 
@end // Car 

@categories

A category is a way to add new methods to existing classes.

@interface

The declaration of a category looks a lot like the declaration for a class:

@interface NSString (NumberConvenience) 
- (NSNumber *) lengthAsNumber; 
@end // NumberConvenience 

You should notice a couple of interesting things about this declaration. First, an existing class is mentioned, followed by a new name in parentheses. This means that the category is called NumberConvenience, and it adds methods to NSString. Another way to say this is, “We’re adding a category onto NSString called NumberConvenience.” You can add as many categories to a class as you want, as long as the category names are unique. You indicate the class you’re putting the category onto (NSString) and the name of the category (NumberConvenience), and you list the methods you’re adding, followed by @end. You can’t add new instance variables, so there is no instance variable section as there is with a class declaration.

@implementation

It comes as no surprise that the @interface section has an @implementation companion. You put the methods you’re writing in @implementation:

@implementation NSString (NumberConvenience) 
- (NSNumber *) lengthAsNumber 
{ 
  unsigned int length = [self length]; 
  return ([NSNumber numberWithUnsignedInt: length]); 
} // lengthAsNumber 
@end // NumberConvenience

Like the @interface for the category, the @implementation has the names of the class and the category, along with the bodies of the new methods. The lengthAsNumber method gets the length of the string by calling [self length]. You will send the lengthAsNumber message to this string. Then, a new NSNumber is created with the length.