Cocoa:在运行时获取Darwin版本

时间:2016-12-27 18:09:13

标签: ios objective-c macos cocoa darwin

如何在运行时获取darwin版本? 我知道我可以使用[[NSProcessInfo processInfo] operatingSystemVersion]获取macOS或iOS版本,但我想要Darwin版本。

互联网上有are tables将macOS和iOS版本链接到darwin版本,但我想实现这种面向未来的版本。

2 个答案:

答案 0 :(得分:1)

使用uname() POSIX / BSD库函数。您声明一个struct utsname类型的变量并传入其地址。结构的release字段将包含带有Darwin版本号的C字符串,例如" 16.3.0"。如果您希望将各个组件作为整数,则必须自己解析它。

答案 1 :(得分:0)

在@Ken Thomases响应中添加代码

  var systemInfo = utsname()
  uname(&systemInfo)
  let machineMirror = Mirror(reflecting: systemInfo.release)
  let darvinVersionString = machineMirror.children.reduce("") { identifier, element in
    guard let value = element.value as? Int8,
      value != 0 else {
        return identifier
    }

    return identifier + String(UnicodeScalar(UInt8(value)))
  }

示例响应:

19.3.0

enter image description here

还有here a list of available versions