我让这个代码在swift 1.1中工作
不知道怎么修

var deviceModelIdentifier: String {
    var size : UInt = 0 // Swift 1.2: var size : Int = 0 (as Ben Stahl noticed in his answer)
    //sysctlbyname(_: UnsafePointer<Int8>, _: UnsafeMutablePointer<Void>, _: UnsafeMutablePointer<Int>, _: UnsafeMutablePointer<Void>, _: Int)
    sysctlbyname("hw.machine", nil, &size, nil, 0)
    var machine = [CChar](count: Int(size), repeatedValue: 0)
    sysctlbyname("hw.machine", &machine, &size, nil, 0)
    return String.fromCString(machine)!
}

错误:无法使用类型为(String,nil,inout UInt,nil,Int)的参数列表调用“sysctlbyname”
sysctlbyname(“hw.machine”,nil,&size,nil,0)
^
错误:无法使用类型为“(String,inout[(CChar)],inout UInt,nil,Int)”的参数列表调用“sysctlbyname”
sysctlbyname(“hw.machine”&machine,&size,nil,0)
如有任何帮助,我们将不胜感激

最佳答案

解决方案就在代码的注释中:Size现在是Int而不是1.2中的Uint,因此编译如下:

var deviceModelIdentifier: String {
    var size : Int = 0
    sysctlbyname("hw.machine", nil, &size, nil, 0)
    var machine = [CChar](count: size, repeatedValue: 0)
    sysctlbyname("hw.machine", &machine, &size, nil, 0)
    return String.fromCString(machine)!
}

(如果您愿意,也可以写var size : size_t = 0
当您涉水通过不安全的指针锅炉板时,错误消息提示:
注意:应为'(UnsafePointer类型的参数列表,
不可存储指针,不可存储指针,
UnsafeMutablePointer,Int)'

关于swift - sysctlbyname在Swift 1.2中失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29545132/

10-12 04:47