bigfish24/ABFRealmMapView

toObject in viewFor annotation

Closed this issue ยท 3 comments

I'm trying to get an object from an ABFAnnottation, in the previous version I could get this object, but after the update I can not anymore.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        
        if (annotation is MKUserLocation) {
            //if annotation is not an MKPointAnnotation (eg. MKUserLocation),
            //return nil so map draws default view for it (eg. blue dot)...
            return nil
        }
       
        if let annotationABF = annotation as? ABFAnnotation {
            if annotationABF.type == .unique {
                if let element = annotationABF.safeObjects.first {

                    let object = element.toObject(MyObject.self)

                    // my custom pin configurations

                    return pinView
                } else {
                    return nil
                }
                
            } else {

                let view = ABFClusterAnnotationView(annotation: annotationABF, reuseIdentifier: "cluster")
                view.color = UIColor(red:0.093, green:0.741, blue:0.335, alpha:1)
                view.count = UInt(annotationABF.safeObjects.count)
                view.canShowCallout = true
                
                return view
                
            }
        }
        return nil
    }

Whenever it will do the toObject it hangs at this point

extension ABFLocationSafeRealmObject {
    public func toObject<T>(_ type: T.Type) -> T {
        return unsafeBitCast(self.rlmObject(), to: T.self)
    }
}

fatal error: can't unsafeBitCast between types of different sizes

Snap! Same problem for me.

let object = annotationABF.safeObjects.first.rlmObject() as! MyObject
It's working for me.

Exactly the same problem here. Tried @cybkuz solution but the compiler was throwing force cast violation so I ended up having something like

 guard let object = annotationABF.safeObjects.first?.rlmObject() as? YourObject else {
      return nil
}

//do stuff with your object