我有一个返回某些系统值的函数。在Swift 2.2中它工作得很好,但我刚刚升级到Xcode 8和Swift 3,现在它失败了。
之前我在用“CFData”而不是“AnyObject”,它可以成功构建,但在运行时出现以下错误:
func ioPlatExpertDevString(_ property: String) -> String {
// Start tapping in to the IO Service
let ioPlatformExpertDevice:io_service_t? = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"))
var sAnswer = "Not Found"
let cfAnswer:AnyObject! = IORegistryEntryCreateCFProperty(ioPlatformExpertDevice!, "\(property)" as CFString!, kCFAllocatorDefault, 0).takeRetainedValue()
if (cfAnswer != nil) {
let nsAnswer = (cfAnswer as! NSString)
sAnswer = nsAnswer.uppercased
}
// Close the IO Service
IOObjectRelease(ioPlatformExpertDevice!)
return sAnswer
}
之前我在用“CFData”而不是“AnyObject”,它可以成功构建,但在运行时出现以下错误:
无法将类型为 '__NSCFData' (0x7fff7b833ec0) 的值转换为类型为 'NSString' (0x7fff7ac32038)。