当我想在我的字典中添加String(beacons[index].major.stringValue)
时,我收到以下错误:
类型的值" [String:String]"没有会员' major'
if(beacons.count > 0) {
let nearestBeacon:CLBeacon = beacons[0]
for var index = 0; index < beacons.count; index++ {
let uuidString = "00000000-0000-0000-0000-000000000000"
var beacons: [Dictionary<String, String>] = []
beacons.append(["uuid": uuidString, "major": String(beacons[index].major.stringValue), "minor": beacons[index].minor.stringValue, "accurency": String(beacons[index].accuracy)])
Server.beaconsSend(beacons) { // success, data in
print(data)
}
}
}
答案 0 :(得分:2)
这一行
var beacons: [Dictionary<String, String>] = []
正在隐藏原始beacons
数组。这意味着在定义它的范围内(for循环的主体),编译器认为beacons
总是引用字典数组。简单的答案是重命名它:
for var index = 0; index < beacons.count; index++ {
let uuidString = "00000000-0000-0000-0000-000000000000"
var myBeacons: [Dictionary<String, String>] = []
myBeacons.append(["uuid": uuidString, "major": String(beacons[index].major.stringValue), "minor": beacons[index].minor.stringValue, "accurency": String(beacons[index].accuracy)])
Server.beaconsSend(myBeacons) { // success, data in
print(data)
}
}
答案 1 :(得分:0)
可能是因为您在信标数组上遇到条件(如果没有)时创建了信标字典。 您创建的信标现在隐藏了信标数组,而您只是引用字典