i want to pass some extra information through CLBeacon
so i create a subclass of it called CLBeaconExtender
. the problem is while executing the code, it give me this error
NSArray element failed to match the Swift Array Element type
this is the code:
Subclass of CLBeacon
class CLBeaconExtender: CLBeacon{
var id: String!
}
MainClass
class beaconController_1: UIViewController, CLLocationManagerDelegate {
var beacon_collection: UICollectionView!
var location_manager = CLLocationManager()
var beacon_manager = [CLBeaconExtender]()
var beacon_region = [CLBeaconRegion]()
// Other code that i don't paste
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
if !beacons.isEmpty {
beacon_manager = beacons as! [CLBeaconExtender]
beacon_manager[0].id = "hello world" // here appear the error
beacon_collection.reloadData()
}
else{
if beacon_manager.count != 0 {
beacon_manager = beacons as! [CLBeaconExtender]
beacon_collection.reloadData()
}
}
}
can anyone solve me this problem or show me an example?
Edit
i think the error is caused by this line:
beacon_manager = beacons as! [CLBeaconExtender]
Edit 2
Sorry for my english, my goal is to create a collectionview
that display all the beacons around me, and update the list every time that didRangBeacons
is called, in addition every collectionviewcell
should contain a specific file audio referring to the respective beacon. So when i tap a specific cell of a specific beacon it start to play a specific sound. My problem is handle the beacon_manager
array. In the first cycle of didRangeBeacons
everything goes fine because !beacon_manager.indices.contains(i)
return !false
so it will execute the if
condition, and it will populate all the beacon_manager
. than in the second cycle of didRangeBeacons
the if condition will return !true
so the code executed will be the else
condition. here the system will crash executing
beacon_manager.remove(at: i)
where i
is 0 and beacon_manager[0] exist.
the crash show me the AppDelegate class with the error:
EXEC_BAD_ACCESS code=1 address= 0x8
Hope you'll understand it now.
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
if !beacons.isEmpty {
var i = 0
for beacon in beacons {
let bx = CLBeaconExtender(b: beacon, sID: "hello world")
if(!beacon_manager.indices.contains(i)){ //check if beacon_manager array exist at i if no create it, if yes update it
beacon_manager.insert(bx, at: i)
}
else{
beacon_manager.remove(at: i)
beacon_manager.insert(bx, at: i)
}
print(beacon_manager[i].id)
i+=1
}
print(beacon_manager.count,beacons.count)
beacon_collection.reloadData()
}
else{
if beacon_manager.count != 0 {
//do other things
}
}
}