0

I'm trying to send back my NSManagedObject as a Serialized object to my webservice. This is how I do it.

    NSDictionary *menuDictionary = [self dictionaryFromMenu:appointment];

        NSError *err;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:menuDictionary options:NSJSONWritingPrettyPrinted error:&err];

        NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                     encoding:NSUTF8StringEncoding];


- (NSDictionary *)dictionaryFromMenu:(Appointment*)appointment {
   NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[appointment.app_id description],@"Id",
     appointment.app_addressinfo, @"AddressInfo",
     appointment.app_completed, @"Completed",
     appointment.app_description, @"Description",
     appointment.app_end, @"EndDate",
     appointment.app_fullday, @"FullDay",
     appointment.app_label, @"Label",
     appointment.app_label_id, @"LabelId",
     appointment.app_location, @"Location",
     appointment.app_private, @"Private",
     appointment.app_project_name, @"ProjectName",
     appointment.app_project_number, @"ProjectNumber",
     appointment.app_relation_address_city, @"RelationAddressCity",
     appointment.app_relation_address_id, @"RelationAddressId",
     appointment.app_relation_address_name, @"RelationAddressName",
     appointment.app_relation_address_street, @"RelationAddressStreet",
     appointment.app_relation_code, @"RelationCode",
     appointment.app_relation_contact_id, @"RelationContactPersonId",
     appointment.app_relation_contact_name, @"RelationContactPersonName",
     appointment.app_relation_name, @"RelationName",
     appointment.app_reminder_info, @"ReminderInfo",
     appointment.app_start, @"StartDate",
     appointment.app_state, @"State",
     appointment.app_subject, @"Subject",
     appointment.app_supplier_code, @"SupplierCode",
     appointment.app_supplier_contact_person_id, @"SupplierContactPersonId",
     appointment.app_supplier_contact_person_name, @"SupplierContactPersonName",
     appointment.app_supplier_name, @"SupplierName",
     appointment.app_type, @"Type",
     nil];
    return dict;
}

And this is how my entity looks like.

enter image description here

Now I get this error:

 *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Appointment 0x16dcb520> valueForUndefinedKey:]: the entity Appointment is not key value coding-compliant for the key "SupplierName".'

Any help ?

Steaphann
  • 2,797
  • 6
  • 50
  • 109

1 Answers1

1

So the value for SupplierName is nil. That causes the construction of your dictionary to stop prematurely. You would have gotten a run time error if you had used the new @{}syntax.

You will need to change your code so for each key, if the value is nil, you use [NSNull null] instead.

David H
  • 40,852
  • 12
  • 92
  • 138