我有一個函數返回某些系統值。它在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).
對於其財產,你調用該函數? –