Swift函数删除了“未知”的信标 - ibeacon

时间:2017-03-17 17:04:19

标签: swift ibeacon

func filteredBeacons(_ beacons: [CLBeacon]) -> [CLBeacon] {
    var filteredBeacons = beacons   // Copy

    var lookup = Set<String>()
    for index in 0..<beacons.count {


        let currentBeacon = beacons[index]
        let identifier = "\(currentBeacon.major)/\(currentBeacon.minor)"

        if lookup.contains(identifier) {
            filteredBeacons.remove(at: index)
        } else {
            lookup.insert(identifier)
        }

    }

    return filteredBeacons
}

我的理解是此代码将删除任何重复的信标。我现在想要这个代码也删除接近“未知”的信标(即很远)。有人能告诉我怎么做吗?我知道需要CLProximity课程。

我是xcode的新手,如果可能的话,我会很感激。谢谢。

2 个答案:

答案 0 :(得分:1)

要移除任何已接近unknown的信标,您只需在循环中添加以下代码段:

if currentBeacon.proximity == .unknown {
   filteredBeacons.remove(at: index)
}

顺便说一句CLProximity是一个枚举,因此您只需撰写.unknown而不是CLProximity.unknown

答案 1 :(得分:1)

尝试:

func filteredBeacons(_ beacons: [CLBeacon]) -> [CLBeacon] {
    var filteredBeacons = beacons   // Copy

    var lookup = Set<String>()
    for index in 0..<beacons.count {    
        let currentBeacon = beacons[index]
        if currentBeacon.proximity == .unknown {
          filteredBeacons.remove(at: index)
        }
        else {
          let identifier = "\(currentBeacon.major)/\(currentBeacon.minor)"

          if lookup.contains(identifier) {
              filteredBeacons.remove(at: index)
          } else {
              lookup.insert(identifier)
          }
        }
    }

    return filteredBeacons
}