7

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?

Cesare
  • 9,139
  • 16
  • 78
  • 130
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
  • 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 May 05 '15 at 21:33
  • 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 `CLLocationCoordinate2D` – Kyle Decot May 06 '15 at 13:22
  • 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 May 14 '15 at 19:22

2 Answers2

0

Make your property dynamic

dynamic var coordinate: CLLocationCoordinate2D?
Silmaril
  • 4,241
  • 20
  • 22
-1

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.

Miralem Cebic
  • 1,276
  • 1
  • 15
  • 28