If you want to create the cache once and check against it, I generally use an +initialize
method. This method is called before the first message sent to the class, so the cache would be created before +function::
(which, by the way, is a terrible selector name) could be called. In this case, I usually declare the cache variable in the .m file, but declaring it in the method definition may also work.
Edit: Adding an example at request of OP:
// MyClass.m
static NSMutableDictionary* cache;
+ (void) initialize {
cache = [[NSMutableDictionary alloc] init];
}
+ (double) cachedValueForParam1:(id)param1 param2:(id)param2 {
// Test if (param1,param2) is in cache and return cached value.
}
Obviously, if a value doesn't exist in the cache, you should have some code that adds the value. Also, I have no idea how you intend to combine param1
and param2
as the key for the cache, or how you'll store the value. (Perhaps +[NSNumber numberWithDouble:]
and -[NSNumber doubleValue]
?) You'll want to make sure you understand dictionary lookups before implementing such a strategy.