是否值得使用 ivar 而不是 Gestalt(gestaltSystemVersion, (SInt32*)&systemVersion) ?
我想支持 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Gestalt
非常便宜,尤其是与运行单独的sw_vers -productVersion
来弄清楚这一点相比。也就是说,将其作为静态变量缓存在实现文件中肯定不会有什么坏处。您可以执行以下操作:MDObject.m:
+initialize
在创建该类的实例之前被调用一次,并且(通常)仅调用一次。因此,它提供了一个方便的地方来确保在实际使用任何对象之前正确确定静态变量。Gestalt
is incredibly cheap, especially when compared to something like running a separatesw_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:
+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.