在Mac OS X上使用Swift检查OS版本 [英] Check OS version using Swift on Mac OS X
问题描述
我正在构建一个应用程序,该应用程序需要检查Mac上安装了什么版本的OSX.它是针对10.9小牛构建的,但是所有的Swift API都只有10.10.
I'm building an application that needs to check what version of OS X is installed on a Mac. It is built against 10.9 Mavericks, but all of the Swift APIs are pretty much 10.10 only.
我正在尝试使用以下内容,但是由于它没有这些API,因此无法针对10.9 SDK进行编译,我知道如何使用if #available(OSX 10.10, *)
根据不同版本进行条件编译,但是,因为
I'm trying to use the following, however it does not compile against the 10.9 SDK since it does not have these APIs, I know how to use if #available(OSX 10.10, *)
to conditionally compile as per the different versions, however because NSProcessInfo
is a 10.10+ API I can't use it to check to see if the Mac is running 10.9. This is the code I have so far (however it does not compile against the 10.9 SDK):
let osVersion = NSProcessInfo.processInfo().operatingSystemVersion
if osVersion.minorVersion == 9 {
// Do something for OS X 10.9 Mavericks
} else {
// Do something for anything not Mavericks (10.10+)
}
还有另一种方法可以检查Mac在10.9中运行的操作系统吗?基本上,我可以将上面的代码包装为:
Is there another way to check what OS the Mac is running in 10.9? Essentially I could wrap the above code with:
if #available(OSX 10.10, *) {
let osVersion = NSProcessInfo.processInfo().operatingSystemVersion
// Check OS version
} else {
// Check OS version using a 10.9 API
}
我只是不知道该代码块的// Check OS version using a 10.9 API
部分要填写什么.
I just don't know what to fill in for the // Check OS version using a 10.9 API
section of that code block.
推荐答案
如果在其他版本中使用其他版本,则将使用相同的检查,只是将10.9代码放入其中
The same check will be used just put 10.9 code inside if for other version in else
if #available(OSX 10.9, *) {
} else {
}
这篇关于在Mac OS X上使用Swift检查OS版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!