macos 如何获取OS X系统版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/885911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the OS X system version?
提问by jin
I want to get the OS X system version, such as: 10.5.4, 10.4.8, etc. I want to get it in my app, how do I do this? Thanks!
我想获取 OS X 系统版本,例如:10.5.4、10.4.8 等,我想在我的应用程序中获取它,我该怎么做?谢谢!
回答by altan
You can read the property list at "/System/Library/CoreServices/SystemVersion.plist and extract the "ProductVersion" key, this is how the OS X installer application does it. Here's an example:
您可以阅读“/System/Library/CoreServices/SystemVersion.plist”中的属性列表并提取“ProductVersion”键,这是 OS X 安装程序应用程序的工作方式。这是一个示例:
NSString *versionString;
NSDictionary * sv = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
versionString = [sv objectForKey:@"ProductVersion"];
Alternatively, the command swvers -productVersion
will do the same.
或者,该命令swvers -productVersion
将执行相同的操作。
回答by Marc Charbonneau
You can use Gestalt:
您可以使用格式塔:
SInt32 version = 0;
Gestalt( gestaltSystemVersion, &version );
BOOL leopard = ( version >= 0x1050 );
if ( leopard )
{
//draw it this way
}
else
{
//draw it that way
}
Keep in mind if you're checking if a method is available or not, it's better to test that directly using respondsToSelector:.
请记住,如果您正在检查某个方法是否可用,最好直接使用 respondsToSelector: 进行测试。
回答by Nick Moore
NSString *osver()
{
SInt32 versionMajor=0, versionMinor=0, versionBugFix=0;
Gestalt(gestaltSystemVersionMajor, &versionMajor);
Gestalt(gestaltSystemVersionMinor, &versionMinor);
Gestalt(gestaltSystemVersionBugFix, &versionBugFix);
return [NSString stringWithFormat:@"%d.%d.%d", versionMajor, versionMinor, versionBugFix];
}
回答by Vadim
-[NSProcessInfo operatingSystemVersionString]
is human readable and localized. Appropriate for displaying to user or using in bug emails and such, but not appropriate for parsing.
-[NSProcessInfo operatingSystemVersionString]
是人类可读和本地化的。适用于向用户显示或在错误电子邮件等中使用,但不适用于解析。
回答by BJ Homer
Again, you can use Gestalt. Look at the documentation for more information; specifically, you'll want to pass the gestaltSystemVersionMajor
, gestaltSystemVersionMinor
, and gestaltSystemVersionBugFix
constants in the "System Version Constants" portion of the Gestalt Manager Reference documentation
同样,您可以使用格式塔。查看文档以获取更多信息;具体地讲,你要通过gestaltSystemVersionMajor
,gestaltSystemVersionMinor
和gestaltSystemVersionBugFix
常量在的“系统版本常量”部分完形经理参考文档
回答by smorgan
There's also a Cocoa wrapper around the Gestalt calls others have mentioned in the Google Toolbox for Mac: http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMSystemVersion.h
其他人在 Mac 版 Google 工具箱中提到的格式塔调用周围还有一个 Cocoa 包装器:http: //code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMSystemVersion.h
回答by NewStack
use this method it will return Mac OS X version
使用此方法将返回 Mac OS X 版本
+(SInt32) OSVersion;
{
SInt32 osxMinorVersion;
Gestalt(gestaltSystemVersionMinor, &osxMinorVersion);
return osxMinorVersion;
}
回答by Mykhailo Lysenko
After 10_10, 8_0 were presented the better & simplest way would be[NSProcessInfo processInfo].operatingSystemVersion
which will returnNSOperatingSystemVersion
struct
with all 3 numbers.
在 10_10、8_0 之后,更好且最简单的方法是[NSProcessInfo processInfo].operatingSystemVersion
返回
带有所有 3 个数字的NSOperatingSystemVersion
结构体
。