I am writing an iPhone app and I want to create a NSCache singleton.
I am having trouble, here's the code that I have:
MyAppCache.h:
#import <Foundation/Foundation.h>
@interface MyAppCache : NSCache {}
+ (MyAppCache *) sharedCache;
@end
MyAppCache.m:
#import "SpotmoCache.h"
static MyAppCache *sharedMyAppCache = nil;
@implementation MyAppCache
+ (MyAppCache *) sharedCache {
if (sharedMyAppCache == nil) {
sharedMyAppCache = [[super allocWithZone:NULL] init];
}
return sharedMyAppCache;
}
+ (id)allocWithZone:(NSZone *)zone {
return [[self sharedCache] retain];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (NSUInteger)retainCount {
return NSUIntegerMax; //denotes an object that cannot be released
}
- (void)release{
//do nothing
}
- (id)autorelease {
return self;
}
@end
When I want to add something or get something from the cache I might write:
#import "MyAppCache.h"
MyAppCache *theCache = [MyAppCache sharedCache];
Then:
NSData *someData = [[theCache objectForKey: keyString] retain];
Or:
[theCache setObject: someData forKey: keyString cost: sizeof(someData)];
The problem: the compiler complains 'MyAppCache' may not respond to 'method' for each of those lines.
I might be doing something completely wrong here - know how to make this work?