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?
CLLocationCoordinate2D
– Kyle DecotRKAttributeMapping
(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 sincecoordinate
is optional (& thusnil
) andcoordinate
being used for both keypaths (& thus both arenil
). – Blake Merryman