macos 在目标 c 中查找 Mac OS X 版本号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6492038/
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
Find Mac OS X version number in objective c
提问by AmaltasCoder
How can I find the version number of Mac OS X (eg. "10.6.7") from my Cocoa Objective-C application?
如何从我的 Cocoa Objective-C 应用程序中找到 Mac OS X 的版本号(例如“10.6.7”)?
回答by
#import <CoreServices/CoreServices.h>
SInt32 major, minor, bugfix;
Gestalt(gestaltSystemVersionMajor, &major);
Gestalt(gestaltSystemVersionMinor, &minor);
Gestalt(gestaltSystemVersionBugFix, &bugfix);
NSString *systemVersion = [NSString stringWithFormat:@"%d.%d.%d",
major, minor, bugfix];
回答by Matt Gallagher
You could use the same technique that Apple's code uses...
您可以使用 Apple 代码使用的相同技术...
NSDictionary *systemVersionDictionary =
[NSDictionary dictionaryWithContentsOfFile:
@"/System/Library/CoreServices/SystemVersion.plist"];
NSString *systemVersion =
[systemVersionDictionary objectForKey:@"ProductVersion"];
Apple does exactly this to fill in the version number for various system utilities in the function _CFCopySystemVersionDictionary
here:
Apple 正是这样做的,以在_CFCopySystemVersionDictionary
此处的函数中填写各种系统实用程序的版本号:
回答by taichino
For OS X 10.10+ I think using NSProcessInfo
is an easier and safer way to do that:
对于 OS X 10.10+,我认为使用NSProcessInfo
是一种更简单、更安全的方法:
NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
NSLog([NSString stringWithFormat:@"%ld.%ld.%ld", version.majorVersion, version.minorVersion, version.patchVersion]);