对成员 'subscript' 的引用不明确
Ambiguous reference to member 'subscript'
我的代码出现了这个错误:
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]
但出现了同样的错误。
我在 Whosebug 上看到了一些关于此错误的问题,但所有答案都不适用于我的代码。
我正在使用 Xcode 8 Beta 6 和 Swift3。在 Swift 2 中,此代码完美运行。
感谢任何帮助:-)
您不能转换为无类型的 Swift 字典或数组。变化
as Dictionary
as Array
到
as NSDictionary
as NSArray
或者,如果您知道实际类型(即这是一个 of 的数组是什么?这是一个 of 的字典是什么?) , 转换为那些实际类型。
另一种方法是使用新的无类型类型,[Any]
和 [AnyHashable:Any]
。但根据我的经验,这可能有点棘手。基本上 Apple 已经取消了 Objective-C 和 Swift 之间的自动桥接,并用 "permissive boxing" 取而代之,这会导致一些类型不匹配,您必须自己进行补偿。
我找到了这个
我遇到了同样的问题
if let paymentDict = dict["payment"] as? Dictionary
{
self.payment = OrderPayment(dict: paymentDict)
}
然后我添加更具体类型的词典
if let paymentDict = dict["payment"] as? Dictionary<String,AnyObject>
{
self.payment = OrderPayment(dict: paymentDict)
}
对我有用
我的代码出现了这个错误:
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]
但出现了同样的错误。
我在 Whosebug 上看到了一些关于此错误的问题,但所有答案都不适用于我的代码。
我正在使用 Xcode 8 Beta 6 和 Swift3。在 Swift 2 中,此代码完美运行。
感谢任何帮助:-)
您不能转换为无类型的 Swift 字典或数组。变化
as Dictionary
as Array
到
as NSDictionary
as NSArray
或者,如果您知道实际类型(即这是一个 of 的数组是什么?这是一个 of 的字典是什么?) , 转换为那些实际类型。
另一种方法是使用新的无类型类型,[Any]
和 [AnyHashable:Any]
。但根据我的经验,这可能有点棘手。基本上 Apple 已经取消了 Objective-C 和 Swift 之间的自动桥接,并用 "permissive boxing" 取而代之,这会导致一些类型不匹配,您必须自己进行补偿。
我找到了这个
我遇到了同样的问题
if let paymentDict = dict["payment"] as? Dictionary
{
self.payment = OrderPayment(dict: paymentDict)
}
然后我添加更具体类型的词典
if let paymentDict = dict["payment"] as? Dictionary<String,AnyObject>
{
self.payment = OrderPayment(dict: paymentDict)
}
对我有用