You can do the same in Swift (error checking omitted for brevity):
func platform() -> String {
var size : Int = 0 // as Ben Stahl noticed in his answer
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)!
}
Update for Swift 3:
func platform() -> String {
var size = 0
sysctlbyname("hw.machine", nil, &size, nil, 0)
var machine = [CChar](repeating: 0, count: size)
sysctlbyname("hw.machine", &machine, &size, nil, 0)
return String(cString: machine)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…