在 Swift 中检查操作系统版本? [英] Check OS version in Swift?

查看:34
本文介绍了在 Swift 中检查操作系统版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Swift 中检查系统信息.我发现,它可以通过代码实现:

I'm trying to check system information in Swift. I figured out, that it could be achieved by code:

var sysData:CMutablePointer<utsname> = nil
let retVal:CInt = uname(sysData)

这段代码有两个问题:

  1. sysData 的初始值应该是多少?此示例在 retVal 中给出 -1,可能是因为 sysData 为零.
  2. 如何从 sysData 读取信息?

推荐答案

对于 iOS,请尝试:

For iOS, try:

var systemVersion = UIDevice.current.systemVersion

对于 OS X,请尝试:

For OS X, try:

var systemVersion = NSProcessInfo.processInfo().operatingSystemVersion

如果您只想检查用户是否至少运行了特定版本,您还可以使用以下适用于 iOS 和 OS X 的 Swift 2 功能:

If you just want to check if the users is running at least a specific version, you can also use the following Swift 2 feature which works on iOS and OS X:

if #available(iOS 9.0, *) {
    // use the feature only available in iOS 9
    // for ex. UIStackView
} else {
    // or use some work around
}

但不建议检查操作系统版本.最好检查您要使用的功能是否在设备上可用,而不是比较版本号.对于 iOS,如上所述,您应该检查它是否响应选择器;例如:

BUT it is not recommended to check the OS version. It is better to check if the feature you want to use is available on the device than comparing version numbers. For iOS, as mentioned above, you should check if it responds to a selector; eg.:

if (self.respondsToSelector(Selector("showViewController"))) {
    self.showViewController(vc, sender: self)
} else {
    // some work around
}

这篇关于在 Swift 中检查操作系统版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆