Skip to content

Instantly share code, notes, and snippets.

@gmertk
Created May 27, 2015 10:00
Show Gist options
  • Save gmertk/0c19f614f320296b99b6 to your computer and use it in GitHub Desktop.
Save gmertk/0c19f614f320296b99b6 to your computer and use it in GitHub Desktop.
Use sets for regions in didRangeBeacons
func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {
var latestRegions = Set<CLBeaconRegion>()
// Create CLBeaconRegions from CLBeacons
if let beacons = beacons as? [CLBeacon] {
for beacon in beacons {
latestRegions.insert(regionFromBeacon(beacon))
}
}
let enteredRegions = latestRegions.subtract(currentRegions)
let exitedRegions = currentRegions.subtract(latestRegions)
currentRegions = latestRegions
if enteredRegions.count > 0 {
println("Entered")
println(enteredRegions)
}
if exitedRegions.count > 0 {
println("Exited")
println(exitedRegions)
}
}
func regionFromBeacon(beacon: CLBeacon) -> CLBeaconRegion {
let major = CLBeaconMajorValue(beacon.major.integerValue)
let minor = CLBeaconMinorValue(beacon.minor.integerValue)
let identifier = "\(beacon.proximityUUID.UUIDString).\(major).\(minor)" // Used for "is equal" check in Sets
return CLBeaconRegion(proximityUUID: beacon.proximityUUID, major: major, minor: minor, identifier: identifier)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment