Last modified 27 June 2003 by jdavis@math.wisc.edu

SMKResource

Inherits from NSObject

SMKResource is the class of object that can be stored in an SMKResourceManager. You subclass SMKResource to endow it with information useful to you. Each resource is identified by a numeric key. It stores its key and its resource manager, which you can access through the following methods.

- (id)setKey:(int)key manager:(SMKResourceManager *)resourceManager;

Sets the key and resource manager, retaining the latter.

- (int)key;

Returns the resource's key.

- (id)manager;

Returns the resource manager to which the resource belongs.

When an SMKResource deallocates, it automatically notifies its resource manager. So, to discard a resource, you simply release or autorelease it as usual. But how do you get a resource in the first place? Resources are typically created when an SMKResourceManager invokes this class method:

+ (id)resourceWithKey:(int)key manager:(SMKResourceManager *)resourceManager;

Returns an autoreleased resource with the indicated key and resource manager. If resource initialization fails for some reason, then nil is returned. Subclasses should override this method without calling SMKResource's version, but always calling setKey:manager: somewhere in the overriding version.

Typically, a subclass of SMKResource will also perform some non-trivial initialization of the resource in this method, based on the value of the key. Often the initialization consists of reading default data from a file. By convention, the file is at "class/key.class", inside the Resources directory of the application bundle, where class is the class name and key is the key. You are free to adopt your own conventions, but SMKResource provides a class method to construct this path anyway:

+ (NSString *)standardPathForKey:(int)key;

Returns the absolute path to the standard location for the data file corresponding to the key, as described above.