Realm RealmMapView.toObject — unsafeBitCast returns error — swift 3.0.2
Closed this issue · 2 comments
I've been trying the Building an iOS Clustered Map View for Swift, and every time I get the following error from the code below:
fatal error: can't unsafeBitCast between types of different sizes
The error is being thrown in RealmMapView.toObject(), invoked as shown (copy/paste from the web page, with the addition of ".self" to get rid of a warning):
extension ViewController: MKMapViewDelegate {
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
if let safeObjects = ABFClusterAnnotationView.safeObjectsForClusterAnnotationView(view) {
if let firstObjectName = safeObjects.first?.toObject(ABFRestaurantObject.self).name {
print("First Object: \(firstObjectName)")
}
}
}
}
I've never managed to get around this error. It seems like the framework is trying to convert an annotation to an ABFRestaurantObject, but maybe I'm confused. Is there a workaround for this, or am I just missing something?
The Podfile just references RealmMapView and RealmSwiftSFRestaurantData.
Here is the effect of a pod install:
Using ABFRealmMapView (2.0)
Using RBQFetchedResultsController (5.0.2)
Using RBQSafeRealmObject (1.1)
Using Realm (2.4.4)
Using RealmMapView (2.0)
Using RealmSwift (2.4.4)
Using RealmSwiftSFRestaurantData (0.5)
Using RealmUtilities (0.4)
Using SafeRealmObject (1.1)
Using SwiftFetchedResultsController (5.0.2)
@DallasLyon I'm not sure if this is the correct solution but this has worked for me:
if let firstObject = safeObjects.first?.rlmObject() as? ABFRestaurantObject { print("First Object: \(firstObject.name)") }
The maintainer could probably confirm whether or not this is the intended behavior due to some API changes or if I just read their source code and hacked my own solution! Not sure the rlmObject() returned from this method.
See this code in the OBJ-C example:
Excellent, @jimjeffers. I had sort of gone down the path toward using rlmObject(), but apparently saw something shiny before getting to it. That works. Great.