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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:07:50  来源:igfitidea点击:

Find Mac OS X version number in objective c

objective-ccocoamacos

提问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 _CFCopySystemVersionDictionaryhere:

Apple 正是这样做的,以在_CFCopySystemVersionDictionary此处的函数中填写各种系统实用程序的版本号:

http://opensource.apple.com/source/CF/CF-744/CFUtilities.c

http://opensource.apple.com/source/CF/CF-744/CFUtilities.c

回答by taichino

For OS X 10.10+ I think using NSProcessInfois 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]);