NSNull handling for NSManagedObject properties values

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

    I was stuck with the same problem, found this post, did it in a slightly different way.Using category only though -

    Make a new category file for "NSDictionary" and add this one method -

    @implementation NSDictionary (SuperExtras)
    
    - (id)objectForKey_NoNSNULL:(id)aKey
    {
        id result = [self objectForKey:aKey];
    
    if(result==[NSNull null])
    {
        return nil;
    }
    
    return result;
    
    }
    
    @end
    

    Later on to use it in code, for properties that can have NSNULL in them just use it this way -

    newUser.email = [loopdict objectForKey_NoNSNULL:@"email"];
    

    Thats it

提交回复
热议问题