objective c - CoreData Optional Relationships and JSON null -
i have coredata model 2 objects. first object (images) contains list of image names. second object (book) has relationship field images object called imageid. data type set int16 , marked optional default value of 0, not every book have image.
i request json representation of book object wcf service. 1 of book objects returned has "imageid" : null value. when converting json object managed object in objective-c following error message:
unacceptable type of value attribute: property = "imageid"; desired type = nsnumber; given type = nsnull; value =
how handle conversion? thought of checking null , setting value 0 doesn't seem correct.
many thanks.
*update * in process of implementing daniels solution discovered other fields returned service null. therefore possible modify nsdictionary category enable traverse , replace instance of nil? using jsonkit json parser , seems return parsed result nsmutabledictionary nsdictionary objects representing json objects. have spent of day trying modify objects returned nsdictionaries immutable. use daniels solution on every field needed checking if service changed , field returned break app. alternatively implement daniels solution on every field incase ever not sure if thats solution performance.
some json libraries let set option json null
s omitted dictionary opposed using nsnull
. if that, work because [thedict objectforkey:@"imageid"]
return nil.
but other that, right thing out nsnull
explicitly. making nsdictionary category handy way it:
@implementation nsdictionary (ddnullgetter) - (id)nonnullobjectforkey:(id)key { id val = [self objectforkey:key]; return (val == [nsnull null] ? nil : val); } @end
Comments
Post a Comment