0

hi i have json with array of object, and add property mapping it produce error

my json

{

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

and my model is

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

and my submodel is

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

and my object mapping is

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

and i got maaping issue

*** 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
Wain
  • 118,658
  • 15
  • 128
  • 151
sabeer
  • 603
  • 10
  • 28
  • What's the issue then? Your question seems to just stop... Give full details of log messages related to the error, exception message, stack trace, etc – Wain Dec 07 '15 at 10:45
  • hi wain i added my error on edit – sabeer Dec 07 '15 at 11:07
  • hi @Wain i need one to many relationship mapping – sabeer Dec 07 '15 at 11:58
  • The error says `FirstSwiftApp.subModel` doesn't have an instance variable called `id`. Have you checked the naming? Is the code in the question a copy-paste? – Wain Dec 07 '15 at 11:58
  • One to many how, if you get that wrong each new addition could replace the previous setting (not that that causes the exception) – Wain Dec 07 '15 at 11:59
  • above implementation is correct @Wain for one to many – sabeer Dec 07 '15 at 12:01
  • Thanks @wain its working and i need http://stackoverflow.com/questions/34000051/how-to-set-directly-nested-keypath-in-restkit – sabeer Dec 07 '15 at 12:04
  • A similar discussion might help: https://stackoverflow.com/questions/44690489/how-to-add-objects-of-entity-classdtos-value-using-addattributemappings-in/44775328#44775328 – ABM Jun 27 '17 at 09:09

0 Answers0