如何快速地rkobjectmapping对象数组

时间:2015-12-07 10:09:27

标签: swift restkit restkit-0.20

我有json与对象数组,并添加属性映射它产生错误

我的json

{

 "Data": [
  {
  "Id": 15,
  "OrgId": 3,
  "Detail": "dhdssdg",
  "Recipients": [
    {
      "Id": 15,
      "Username": "shabeer"
    }
    {
      "Id": 16,
      "Username": "aaa"
    }
    {
      "Id": 17,
      "Username": "bbb"
    }
  ]
}
]
}

我的模特是

class MainModel: NSObject {
  var id: NSNumber?
  var orgId: NSNumber?
  var Detail: String?
  var recipients: [subModel]!
}

我的子模型是

class subModel: NSObject {
  var id: NSNumber?
  var Username: String?
}

我的对象映射是

    let mapping = RKObjectMapping(forClass: MainModel.self)

    mapping.addAttributeMappingsFromDictionary([
        "Id"                      : "id",
        "OrgId"                   : "orgId",
        "Detail"                  : "Detail"
        ])

    let subMapping = RKObjectMapping(forClass: subModel.self)



    subMapping.addAttributeMappingsFromDictionary(
        [
            "Id"                      : "id",
            "Username"                : "Username"
        ]
    )

    mapping.addPropertyMapping(RKRelationshipMapping(fromKeyPath: "Recipients", toKeyPath: "recipients", withMapping: subMapping))

let responseDescriptor = RKResponseDescriptor(
        mapping: mapping
        ,method:RKRequestMethod.GET
        ,pathPattern : nil
        ,keyPath :"Data"
        ,statusCodes  :  NSIndexSet(index:201))

我遇到了maaping问题

*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<FirstSwiftApp.subModel 0x7fd682773c30> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key id.'
*** First throw call stack:
(
0   CoreFoundation    0x000000010a4b6f45 __exceptionPreprocess + 165
1   libobjc.A.dylib   0x0000000109f2edeb objc_exception_throw + 48
2   CoreFoundation    0x000000010a4b6b89 -[NSException raise] + 9
3   Foundation        0x0000000109afaa6b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
4   FirstSwiftApp     0x00000001056f67a5 -[RKMappingOperation applyAttributeMapping:withValue:] + 1813
5   FirstSwiftApp     0x00000001056f7455 -[RKMappingOperation applyAttributeMappings:] + 1381
6   FirstSwiftApp     0x00000001056fdcbd -[RKMappingOperation main] + 3485

0 个答案:

没有答案