user523234
user523234

Reputation: 14834

Repeated code to check for iOS version

I use this snippet repeatedly in my app. So, it is better to read it once and store it in the global variable or appDelegate or just keep executing the snippet whenever needed?

#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
if (SYSTEM_VERSION_LESS_THAN(@"7.0"))
    return NO;
return YES;

Upvotes: 1

Views: 430

Answers (2)

bandejapaisa
bandejapaisa

Reputation: 26972

A contrived example, as in this case is completely unnecessary, but if you want to use this pattern in future to ensure something is only called once, use dispatch_once from GCD:

- (BOOL) isIOS7 {
    static dispatch_once_t onceToken;
    __block BOOL isIOS7 = NO;
    dispatch_once(&onceToken, ^{
        isIOS7 = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0");
    });
    return isIOS7;
} 

Upvotes: 1

Undo
Undo

Reputation: 25697

I would say to just keep it in a variable owned by AppDelegate - it's a simple BOOL, so it's not storage intensive.

However, this snippet is not system intensive at all - it's pretty much free performance-wise. Since you are thinking about this kind of optimization, though, it would be better to keep it in AppDelegate.

Upvotes: 5

Related Questions