开发者

Does it worth to have a ivar instead of doing Gestalt(gestaltSystemVersion, (SInt32*)&systemVersion)?

I want to support OSX 10.6 and 10.7 so I am doing some things conditionally. some of them are done several times in very short periods of time so I wonder if there is a gain in having a ivar that will tell me the systemVersion instead of doing

SInt32 systemVersion
Gestalt(gestaltSystemVersion, (SInt32*)&systemVersion);

That will be used :

if (systemVersion >= 0x1070){ //OSX 10.7
}else{//OSX 10.6 
}

I've never used Gestalt in th开发者_运维百科e past. Is Gestalt doing some kind of hard stuff or is cheap to call it every time?


Gestalt is incredibly cheap, especially when compared to something like running a separate sw_vers -productVersion to figure it out. That said, it certainly wouldn't hurt to cache it as a static variable in your implementation file. You could do something like this:

MDObject.m:

enum {
    MDUndeterminedVersion    = 0,
    MDTiger                  = 0x1040,
    MDLeopard                = 0x1050,
    MDSnowLeopard            = 0x1060,
    MDLion                   = 0x1070,
    MDMountainLion           = 0x1080,
    MDMavericks              = 0x1090,
    MDUnknownVersion         = 0x1100 // ??
};

static SInt32 MDSystemVersion = MDUndeterminedVersion;

@implementation

+ (void)initialize {
    if (MDSystemVersion == MDUndeterminedVersion) {
        SInt32 MDFullSystemVersion = 0;
        Gestalt(gestaltSystemVersion, &MDFullSystemVersion);
        MDSystemVersion = MDFullSystemVersion & 0xfffffff0;
    }
}

- (void)someMethod {
   if (MDSystemVersion >= MDLion) {

   } else {

   }
}

@end

+initialize is called once and (usually) only once, before an instance of that class is ever created. So, it provides a convenient place to make sure the static variable is properly determined before any of the objects are actually used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜