gpt4 book ai didi

macos - 对成员 'subscript' 的不明确引用

转载 作者:行者123 更新时间:2023-12-04 16:27:00 26 4
gpt4 key购买 nike

我的代码出现此错误:

func getBatteryInfos(){
let snapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue()

// Pull out a list of power sources
let sources = IOPSCopyPowerSourcesList(snapshot).takeRetainedValue() as Array

// For each power source...
for ps in sources {
// Fetch the information for a given power source out of our snapshot
let info = IOPSGetPowerSourceDescription(snapshot, ps).takeUnretainedValue() as Dictionary

// Pull out the capacity
if let

capacity = info[kIOPSCurrentCapacityKey] as? Double, //Ambiguous reference to member 'subscript'
let charging = info[kIOPSIsChargingKey] as? Int{ //Ambiguous reference to member 'subscript'

batteryPercentage = capacity
batteryState = charging

print("Current battery percentage \(batteryPercentage)")
print("Current state \(batteryState)")



}
}

我试图更换 info[kIOPSCurrentCapacityKey]info["kIOPSCurrentCapacityKey"]但发生同样的错误。
我在 StackOverflow 上看到了一些有关此错误的问题,但所有答案都不适用于我的代码。

我正在使用 Xcode 8 Beta 6 和 Swift3。在 Swift 2 中,这段代码运行良好。

任何帮助表示赞赏:-)

最佳答案

您不能强制转换为无类型的 Swift 字典或数组。改变

as Dictionary
as Array


as NSDictionary
as NSArray

或者,如果您知道实际类型(即这是什么数组?这是什么字典?),请转换为这些实际类型。

另一种方法是使用新的无类型类型, [Any][AnyHashable:Any] .但根据我的经验,这可能有点棘手。基本上,Apple 已经取消了 Objective-C 和 Swift 之间的自动桥接,并用“许可装箱”取而代之,这会导致某些类型不匹配,您必须自己进行补偿。

关于macos - 对成员 'subscript' 的不明确引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39130306/

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