【问题标题】:Value of type "[String : String]" has no member 'major'“[String : String]”类型的值没有成员“major”
【发布时间】:2016-05-27 08:55:03
【问题描述】:

当我想在我的字典中添加 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)
        }
    }
}

【问题讨论】:

    标签: iphone string swift types ibeacon


    【解决方案1】:

    这一行

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

    【讨论】:

      【解决方案2】:

      可能是因为您在信标数组的某个条件(如果其他情况下)中创建了信标字典。 您创建的信标现在隐藏了信标数组,您仅引用了字典

      【讨论】:

      • 谢谢你!
      • 乐于助人。请接受为正确答案。
      猜你喜欢
      • 2016-12-03
      • 2019-08-21
      • 2019-02-03
      • 2016-06-08
      • 2017-02-14
      • 2018-07-12
      • 2018-03-14
      • 2019-07-25
      • 1970-01-01
      相关资源
      最近更新 更多