Can you subclass CLCircularRegion

I'm stuck with subclassing a CLCircularRegion


I have a subclass because I need some additional properties.


class MyCircularRegion: CLCircularRegion {


var alreadyEnteredProperty = false

var geoFenceExitTime: Double = 0.0

var geofenceEnterTime: Double = 0.0

var hasVisitStarted = false


override init(center: CLLocationCoordinate2D, radius: CLLocationDistance, identifier: String) {

super.init(center: center, radius: radius, identifier: identifier)

}


init(center: CLLocationCoordinate2D, radius: CLLocationDistance, identifier: String, alreadyEntered: Bool) {

super.init(center: center, radius: radius, identifier: identifier)

self.alreadyEnteredProperty = alreadyEntered

}


required init?(coder aDecoder: NSCoder) {

fatalError("Its all gone wrong")

}

}


all pretty standard.

I init my subclass like so



func updatePlaceTrigger(places: Set<Place>){

let monitoredRegions = Set<CLRegion>(locationManager.monitoredRegions.filter { region in region is MyCircularRegion })

let newPlaces = places.filter{place in place.inventoryId != nil}.map { (place : Place) -> CLRegion in

if let id = place.inventoryId, let location = place.postion, let category = place.metaData?.category{

let location = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)

let region = MyCircularRegion(center: location, radius: category.radius, identifier: id, alreadyEntered: false)

region.notifyOnEntry = true

region.notifyOnExit = true

regionTriggerMapper[region.identifier] = place

return region

}else{

return CLBeaconRegion(proximityUUID: UUID(), identifier: "")

}

}

// Get items to remove and remove them from monitoring

let needToRemove = monitoredRegions.subtracting(newPlaces)

for region in needToRemove {

stopMonitoring(region)

regionTriggerMapper.removeValue(forKey: region.identifier)

}

// Start to monitor all regions

for region in newPlaces {

startMonitoring(region)

}


}


This line is causing me problems


let monitoredRegions = Set<CLRegion>(locationManager.monitoredRegions.filter { region in region is MyCircularRegion })

it always returns 0


but if I set it to this


let monitoredRegions = Set<CLRegion>(locationManager.monitoredRegions.filter { region in region is CLCircularRegion })

monitoerRegions has some elements, but then if I do


let regions = monitoredRegions as? Set<MyCircularRegion>

regions is now nil


Any help would be awesome


Thanks

Replies

You write:

        let monitoredRegions = Set(locationManager.monitoredRegions.filter { region in region is MyCircularRegion })


Maybe I missed something, but why would a monitoredRegion be a MyCircularRegion ? It may be a CircularRegion, but why should it be a subclass ?

Unless you subclass LocationManager itself for this purpose.


For the second question

let regions = monitoredRegions as? Set<MyCircularRegion>

regions is now nil


You cannot cast a superclass to a subclass ; so normal.

But you could probably rebuild the set, item by item.


var setOfMyCircular: Set<MyCircularRegion> = []

for item in monitoredRegions {

let myItem = // initiatialize myItem from item and define complementary values as needed, if item satisfy some conditions you will define to be considered as MyCircularRegion

setOfMyCircular.insert(myItem)

}


Please: don't forget to close the other threads or feedback on what you tested if it does not work. Good luck.