我有一个函数返回某些系统值。它在Swift 2.2中运行良好,我刚刚升级到Xcode 8和Swift 3,现在它失败了。在Swift 3中将NSData转换为NSString?
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”。它建立正常,但在运行时,我得到以下错误:
Could not cast value of type '__NSCFData' (0x7fff7b833ec0) to 'NSString' (0x7fff7ac32038).
对于其财产,你调用该函数? –