7
votes

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?

2
Why do you want it to be optional when it's a plain number? Presumably the compiler doesn't generate accessors for the properties if they're optional...Wain
Not all activities have an attached latitude and longitude so I thought it would make since for it to be an optional. I see your point for regular numbers but I could pose the same question for something more complex like CLLocationCoordinate2DKyle Decot
I have never used RestKit so I'm not sure of the internal workings... however, I was browsing through the source & found this in the implementation for the initializer for RKAttributeMapping (RKAttributeMapping(fromKeyPath: "coordinate", toKeyPath: "coordinate"))... the first line of code in the initializer is: NSAssert(sourceKeyPath || destinationKeyPath, @"Both the source and destination key paths cannot be nil");. To me this would imply the crash is caused since coordinate is optional (& thus nil) and coordinate being used for both keypaths (& thus both are nil).Blake Merryman

2 Answers

0
votes

Make your property dynamic

dynamic var coordinate: CLLocationCoordinate2D?
-1
votes

In my opinion your app crashes because of this lines

let mapping = RKObjectMapping(forClass: self) let coordinateMapping = RKAttributeMapping(fromKeyPath: "coordinate", toKeyPath: "coordinate")

You want to map an optional property/ variable, but it could be that at the time when you calling objectMapping() your coordinate is nil.

Have you tried something like this:

import MapKit

class Activity: NSObject {

    var coordinate: CLLocationCoordinate2D?

    class func objectMapping() -> RKObjectMapping {

        if let c = self. coordinate {
             let mapping = RKObjectMapping(forClass: self)

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

             return mapping
        }
        return nil
    }
}

I'm not sure if this works but I guess let mapping = RKObjectMapping(forClass: self) is trying to Map your whole classActivity and this crashes your code, when coordinate is not set at this time.