如何在 OS X 或 iOS 中确定运行时的操作系统版本(不使用格式塔)?
位于 CarbonCore/OSUtils.h
中的 Gestalt() 函数在 OS X 10.8 Mountain Lion 中已被弃用.
The Gestalt() function located in CarbonCore/OSUtils.h
has been deprecated as of OS X 10.8 Mountain Lion.
我经常使用这个函数在运行时测试 OS X 操作系统的版本(参见下面的玩具示例).
I often use this function to test the version of the OS X operating system at runtime (see the toy example below).
还有哪些其他 API 可用于在 Cocoa 应用程序中在运行时检查 OS X 操作系统版本?
What other API could be used to check the OS X operating system version at runtime in a Cocoa application?
int main() {
SInt32 versMaj, versMin, versBugFix;
Gestalt(gestaltSystemVersionMajor, &versMaj);
Gestalt(gestaltSystemVersionMinor, &versMin);
Gestalt(gestaltSystemVersionBugFix, &versBugFix);
printf("OS X Version: %d.%d.%d
", versMaj, versMin, versBugFix);
}
在 OS X 10.10(和 iOS 8.0)上,您可以使用 [[NSProcessInfo processInfo] operatingSystemVersion]
返回一个 NSOperatingSystemVersion
结构体,定义为
On OS X 10.10 (and iOS 8.0), you can use [[NSProcessInfo processInfo] operatingSystemVersion]
which returns a NSOperatingSystemVersion
struct, defined as
typedef struct {
NSInteger majorVersion;
NSInteger minorVersion;
NSInteger patchVersion;
} NSOperatingSystemVersion;
NSProcessInfo
中还有一个方法可以为你做比较:
There is also a method in NSProcessInfo
that will do the comparison for you:
- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
注意,虽然记录在 OS X 10.10 及更高版本中可用,operatingSystemVersion
和 isOperatingSystemAtLeastVersion:
都存在于 OS X 10.9 (可能是 10.9.2)并按预期工作.这意味着你不能测试 NSProcessInfo
是否响应这些选择器来检查你是在 OS X 10.9 还是 10.10 上运行.
Beware, although documented to be available in OS X 10.10 and later, both operatingSystemVersion
and isOperatingSystemAtLeastVersion:
exist on OS X 10.9 (probably 10.9.2) and work as expected. It means that you must not test if NSProcessInfo
responds to these selectors to check if you are running on OS X 10.9 or 10.10.
在 iOS 上,这些方法仅从 iOS 8.0 开始有效.
On iOS, these methods are effectively only available since iOS 8.0.