我得到了我的代碼此錯誤:曖昧參考成員「下標」
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中,這段代碼完美運行。
任何幫助表示讚賞:-)
我不能對此進行測試因爲我不知道你使用的類型(例如,我不知道IOPSCopyPowerSourcesInfo是什麼),所以你將不得不實驗t並可能調整以獲得正確的結果。例如,你可能需要'as!'而不是'as'。 – matt
非常感謝,在Dictionary和Array之前添加了NS後就成功了。你認爲蘋果爲了更高的精度和安全性而打破了自動橋接嗎? – Lawris
你可以得出你自己的結論。 :) https://github.com/apple/swift-evolution/blob/master/proposals/0072-eliminate-implicit-bridging-conversions.md – matt