samedi 9 mai 2015

RestKit RKObjectMapping Swift Optional

I have a swift model called Activity that can optionally have a coordinate attached to it.

import MapKit

class Activity: NSObject {

    var coordinate: CLLocationCoordinate2D?

    class func objectMapping() -> RKObjectMapping {
        let mapping = RKObjectMapping(forClass: self)

        let coordinateMapping = RKAttributeMapping(fromKeyPath: "coordinate", toKeyPath: "coordinate")
        coordinateMapping.valueTransformer = RKCLLocationCoordinate2DValueTransformer()
        mapping.addPropertyMapping(coordinateMapping)

        return mapping
    }
}

When launching my application this however gives me:

2015-05-05 12:14:30.741 Sample[54338:531558] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key coordinate.'

If I change coordinate to be non-optional and supply a default the application runs.

So my question is how do I use RestKit in swift in regards to Optionals?

Aucun commentaire:

Enregistrer un commentaire