RestKit RKObjectMapping Swift可选

时间:2015-05-05 16:24:52

标签: ios swift restkit

我有一个名为Activity的快速模型,可以选择附加coordinate

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
    }
}

启动我的应用程序时,这给了我:

  

2015-05-05 12:14:30.741示例[54338:531558] ***由于未捕获的异常终止应用程序' NSUnknownKeyException',原因:' [setValue:forUndefinedKey:]:这个类不是关键坐标符合关键值的编码。'

如果我将coordinate更改为非可选项并提供默认值,则应用程序将运行。

所以我的问题是如何在关于Optionals的swift中使用RestKit?

2 个答案:

答案 0 :(得分:0)

设置您的媒体资源dynamic

dynamic var coordinate: CLLocationCoordinate2D?

答案 1 :(得分:-1)

在我看来,你的应用因此行而崩溃

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

您想映射一个可选的属性/变量,但可能是在您调用objectMapping() coordinate时,您的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 } } 为零。

你有没有试过这样的事情:

let mapping = RKObjectMapping(forClass: self)

我不确定这是否有效,但我猜Activity正在尝试映射您的整个班级coordinate,这会导致您的代码崩溃,而var ProcessHandle : THandle; begin ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, False, GetCurrentProcessId()); //Is PROCESS_ALL_ACCESS needed? ShellExecute(0, 'open', 'App2.exe', PChar(IntToStr(ProcessHandle)), '.\', SW_SHOW); end; 未设置此时时间。