NSNull handling for NSManagedObject properties values

后端 未结 5 612
说谎
说谎 2021-01-30 09:07

I\'m setting values for properties of my NSManagedObject, these values are coming from a NSDictionary properly serialized from a JSON file. My problem

5条回答
  •  日久生厌
    2021-01-30 09:43

    Another method is

    -[NSObject setValuesForKeysWithDictionary:]
    

    In this scenario you could do

    [fight setValuesForKeysWithDictionary:dict];
    

    In the header NSKeyValueCoding.h it defines that "Dictionary entries whose values are NSNull result in -setValue:nil forKey:key messages being sent to the receiver.

    The only downside is you will have to transform any keys in the dictionary to keys that are in the receiver. i.e.

    dict[@"winnerID"] = dict[@"winner"];
    [dict removeObjectForKey:@"winner"];
    

提交回复
热议问题