Graphics Programs Reference
In-Depth Information
items = nil;
}
return 0;
}
Build and run the application. Notice that the console now prints a single BNRItem that
was instantiated with the values passed to BNRItem 's designated initializer.
Class methods
Methods come in two flavors: instance methods and class methods. Instance methods (like
init ) are sent to instances of the class, and class methods (like alloc ) are sent to the
class itself. Class methods typically either create new instances of the class or retrieve
some global property of the class. Class methods do not operate on an instance or have
any access to instance variables.
Syntactically, class methods differ from instance methods by the first character in their de-
claration. An instance method uses the - character just before the return type, and a class
method uses the + character.
One common use for class methods is to provide convenient ways to create instances of
that class. For the BNRItem class, it would be nice if you could create a random item so
that you could test your class without having to think up a bunch of clever names. In
BNRItem.h , declare a class method that will create a random item.
@interface BNRItem : NSObject
{
NSString *itemName;
NSString *serialNumber;
int valueInDollars;
NSDate *dateCreated;
}
+ (id)randomItem;
- (id)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber;
Notice the order of the declarations for the methods. Class methods come first, followed
by initializers, followed by any other methods. This is a convention that makes your head-
er files easier to read.
In BNRItem.m , implement randomItem to create, configure, and return a BNRItem
instance. (Again, make sure this method is between the @implementation and @end .)
+ (id)randomItem
Search WWH ::




Custom Search