gpt4 book ai didi

xcode - 在 Swift 3 中将 NSData 转换为 NSString?

转载 作者:行者123 更新时间:2023-12-02 18:05:58 27 4
gpt4 key购买 nike

我有一个返回某些系统值的函数。它在 Swift 2.2 中运行良好,我刚刚升级到 Xcode 8 和 Swift 3,现在失败了。

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).

最佳答案

替换

let nsAnswer = (cfAnswer as! NSString) 

let nsAnswer = NSString(data: cfAnswer as! Data, encoding: String.Encoding.utf8.rawValue)

关于xcode - 在 Swift 3 中将 NSData 转换为 NSString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39619113/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com