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

查看:182
本文介绍了检查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为nil。

  2. 如何从sysData读取信息?


推荐答案

对于iOS,请尝试:

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,如上所述,您应检查它是否响应选择器;
eg。:

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天全站免登陆