是否值得使用 ivar 而不是 Gestalt(gestaltSystemVersion, (SInt32*)&systemVersion) ?

发布于 2024-11-27 13:13:52 字数 350 浏览 2 评论 0原文

我想支持 OSX 10.6 和 10.7,所以我有条件地做了一些事情。 其中一些在很短的时间内完成了几次,所以我想知道拥有一个 ivar 是否有好处,它会告诉我系统版本而不是这样做

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

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

我过去从未使用过格式塔。格式塔是在做某种困难的事情还是每次调用它都很便宜?

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 the past. Is Gestalt doing some kind of hard stuff or is cheap to call it every time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2024-12-04 13:13:52

Gestalt 非常便宜,尤其是与运行单独的 sw_vers -productVersion 来弄清楚这一点相比。也就是说,将其作为静态变量缓存在实现文件中肯定不会有什么坏处。您可以执行以下操作:

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 在创建该类的实例之前被调用一次,并且(通常)仅调用一次。因此,它提供了一个方便的地方来确保在实际使用任何对象之前正确确定静态变量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文