I have a class method loadImage:(NSString *)pathto load image from path, if the path is nil, the load the default path image.
+(NSImage *) loadImage:(NSString *)path{
if(path== nil){
path = [[NSBundle mainBundle] pathForResource:@"default" ofType:@"png"];
}
}
because the default path is always using the same path, I want to calculate the path only once if I run the method 1000 times, like
if(defaultPath == nil){
defaultPath = [[NSBundle mainBundle] pathForResource:@"default" ofType:@"png"];
}
path = defaultPath;
, I think I can use static variable, but I don’t know how to do it, please help me, I’m glad to know any suggestions about improving performance.
Simply use a static variable to hold the default path:
But if you’re doing it for performance reasons, first make sure it’s worth it. Most probably it’s a premature optimization that’s not worth the trouble.