RestKit RKObjectMapping Swift 可选 [英] RestKit RKObjectMapping Swift Optional
问题描述
我有一个名为 Activity
的 swift 模型,它可以选择附加一个 coordinate
.
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] *** 由于未捕获的异常NSUnknownKeyException"而终止应用程序,原因:[setValue:forUndefinedKey:]:此类不是键值编码-符合关键坐标.'
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.'
如果我将 coordinate
更改为非可选并提供应用程序运行的默认值.
If I change coordinate
to be non-optional and supply a default the application runs.
所以我的问题是如何在 Optionals 方面快速使用 RestKit?
So my question is how do I use RestKit in swift in regards to Optionals?
推荐答案
使您的财产动态
dynamic var coordinate: CLLocationCoordinate2D?
这篇关于RestKit RKObjectMapping Swift 可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!