NSNull handling for NSManagedObject properties values

后端 未结 5 613
说谎
说谎 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:51

    It might be a little easier if you wrap this in a macro:

    #define NULL_TO_NIL(obj) ({ __typeof__ (obj) __obj = (obj); __obj == [NSNull null] ? nil : obj; })
    

    Then you can write things like

    fight.winnerID = NULL_TO_NIL([dict objectForKey:@"winner"]);
    

    Alternatively you can pre-process your dictionary and replace all NSNulls with nil before even trying to stuff it into your managed object.

提交回复
热议问题