Graphics Programs Reference
In-Depth Information
Now we have ensured that multiple instances of BNRItemStore cannot be created. We
have also ensured that once the instance of BNRItemStore is created, it is never des-
troyed because a static variable (that never gets destroyed) always maintains ownership of
it.
In BNRItemStore.h , give BNRItemStore an instance variable to hold an array of
BNRItem instances and declare two more methods:
#import <Foundation/Foundation.h>
@class BNRItem;
@interface BNRItemStore : NSObject
{
NSMutableArray *allItems;
}
+ (BNRItemStore *)sharedStore;
- (NSArray *)allItems;
- (BNRItem *)createItem;
@end
See the @class directive? That tells the compiler that there is a BNRItem class and that
it doesn't need to know this class's details in the current file. This allows us to use the
BNRItem symbol in the declaration of createItem without importing BNRItem.h .
Using the @class directive can speed up compile times considerably because fewer files
have to be recompiled when one file changes. (Wonder why? Flip back and read the sec-
tion called “For the More Curious: Build Phases, Compiler Errors, and Linker Errors” . )
In files that actually send messages to the BNRItem class or instances of it, you must im-
port the file it was declared in so that the compiler will have all of its details. At the top of
BNRItemStore.m , import BNRItem.h .
#import "BNRItemStore.h"
#import "BNRItem.h"
In BNRItemStore.m , override init to create an instance of NSMutableArray and
assign it to the instance variable.
- (id)init
{
self = [super init];
if (self) {
allItems = [[NSMutableArray alloc] init];
}
return self;
}
Now implement the two methods in BNRItemStore.m .
Search WWH ::




Custom Search