macos 如何以编程方式确定正在运行的 Mac OS X 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/157759/
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 05:55:47  来源:igfitidea点击:

How can I determine the running Mac OS X version programmatically?

cocoamacosmacos-carbon

提问by Joe Corkery

I have a program which needs to behave slightly differently on Tiger than on Leopard. Does anybody know of a system call which will allow me to accurately determine which version of Mac OS X I am running. I have found a number of macro definitions to determine the OS of the build machine, but nothing really good to determine the OS of the running machine.

我有一个程序需要在 Tiger 和 Leopard 上的行为略有不同。有没有人知道一个系统调用,它可以让我准确地确定正在运行的 Mac OS XI 版本。我找到了许多宏定义来确定构建机器的操作系统,但没有什么能真正确定运行机器的操作系统。

Thanks, Joe

谢谢,乔

回答by Nik Reiman

See this article here

在这里看到这篇文章

But in short, if you're using carbon, use the Gestalt() call, and if you're using cocoa, there is a constant called NSAppKitVersionNumber which you can simply check against.

但简而言之,如果您使用的是 carbon,请使用 Gestalt() 调用,如果您使用的是可可,则有一个名为 NSAppKitVersionNumber 的常量,您可以简单地检查它。

Edit: For Mac OSX 10.8 and above, don't use Gestalt() anymore. See this answer for more details: How do I determine the OS version at runtime in OS X or iOS (without using Gestalt)?

编辑:对于 Mac OSX 10.8 及更高版本,不再使用格式塔()。有关更多详细信息,请参阅此答案:如何在 OS X 或 iOS 中确定运行时的操作系统版本(不使用格式塔)?

回答by Joe Corkery

Could you just check for the presence of a capability? For instance:

你能不能检查一下是否存在某种能力?例如:

if (NSClassFromString(@"NSKeyedArchiver") != Nil)

or

或者

if ([arrayController respondsToSelector: @selector(selectedIndexes)])

then you know that the operating system does what you need it to do, not that Apple's product marketing group gave it a particular number ;-)

那么你就知道操作系统做了你需要它做的事情,而不是苹果的产品营销团队给了它一个特定的数字;-)

回答by S.Lott

The API is through the Gestalt Manager.

API 是通过格式塔管理器实现的。

See "Determining OS Version" in the CocoaDev site.

请参阅CocoaDev 站点中的“确定操作系统版本”。

回答by Douglas F Shearer

In terminal:

在终端:

system_profiler SPSoftwareDataType

Gives:

给出:

Software:

    System Software Overview:

      System Version: Mac OS X 10.5.5 (9F33)
      Kernel Version: Darwin 9.5.0
      Boot Volume: Main
      Boot Mode: Normal
      Computer Name: phoenix
      User Name: Douglas F Shearer (dougal)
      Time since boot: 2 days 16:55

Or:

或者:

sw_vers

Gives:

给出:

ProductName:    Mac OS X
ProductVersion: 10.5.5
BuildVersion:   9F33

回答by Mark Baker

Is the OS version really what you want? There may be a more appropriate thing to test for, such as the presence of, or version number of, a particular framework.

操作系统版本真的是您想要的吗?可能有更合适的测试对象,例如特定框架的存在或版本号。

回答by neoneye

within your program you can use Gestalt. Here is the code I am using for my program to obtain the OS version.

在您的程序中,您可以使用格式塔。这是我用于我的程序以获取操作系统版本的代码。

long version = 0;
OSStatus rc0 = Gestalt(gestaltSystemVersion, &version);
if((rc0 == 0) && (version >= 0x1039)) {      
    // will work with version 10.3.9
    // works best with version 10.4.9
    return; // version is good
}
if(rc0) {
    printf("gestalt rc=%i\n", (int)rc0);
} else {
    printf("gestalt version=%08x\n", version);
}

回答by Joe McMahon

respondsToSelector:almost certainly is better than you maintaining a table of what given releases do and do not implement.

respondsToSelector:几乎可以肯定,这比你维护一个给定版本执行和不执行的表格要好。

Be lazy. Let the runtime tell you whether it can do something or not, and fall back to older methods when you need to. Your code will be far less fragile because you don't have to maintain your own global data that the rest of your code has to keep checking with.

偷懒。让运行时告诉您它是否可以做某事,并在您需要时回退到旧方法。您的代码将不会那么脆弱,因为您不必维护自己的全局数据,而您的代码的其余部分必须不断检查这些数据。

回答by Brock Woolf

Run this in the command line:

在命令行中运行它:

system_profiler SPSoftwareDataType | grep Mac