Search code examples
ioscore-datansnull

NSInvalidArgumentException desired type = NSString; given type = __NSDictionaryM; Unacceptable type of value : property


Here i am setting some values using the NSManagedObject.

First problem i got that when i am trying to assign the null value to the property it is giving error :

desired type = NSString;

given type =null.

I am getting values from NSDictionary serialized from a JSON file. But when the value is null i get the crash giving above error:

desired type = NSString;

given type =null.

For this i am writing the the following code but still i get the error.What should i do to handle null value correctly.

    NSManagedObject *updateDevice=[results lastObject];

           if([results count] > 0){
                        //if(1){
              NSLog(@"updateeeee");
                        //continue;
   [updateDevice setValue:[NSString stringWithFormat:@"%@",[dict objectForKey:@"clip_image_path"]] forKey:@"clip_image_path"];
   [updateDevice setValue:[dict objectForKey:@"clip_name"] forKey:@"clip_name"];
   [updateDevice setValue:[dict objectForKey:@"page_categorisation"] forKey:@"page_categorisation"];

following properties have the null values

  [updateDevice setValue:[dict objectForKey:@"personality_company_master_values"] == [NSNull null] ? nil:dict    forKey:@"personality_company_master_values"];

  [updateDevice setValue:[dict objectForKey:@"category_master_values"] == [NSNull null] ? nil: dict forKey:@"category_master_values"];

  [updateDevice setValue:[dict objectForKey:@"brand_master_values"] == [NSNull null] ? nil:dict forKey:@"brand_master_values"];

  [updateDevice setValue:[dict objectForKey:@"company_master_values"] == [NSNull null] ? nil:dict forKey:@"company_master_values"];

  [updateDevice setValue:[dict objectForKey:@"product_master_values"] == [NSNull null] ? nil:dict forKey:@"product_master_values"];

  [updateDevice setValue:[dict objectForKey:@"industry_master_values"] == [NSNull null] ? nil:dict forKey:@"industry_master_values"];

Solution

  • You are getting the error cited in your question title because this line:

    [updateDevice setValue:[dict objectForKey:@"personality_company_master_values"] == [NSNull null] ? nil:dict    forKey:@"personality_company_master_values"];
    

    is passing dict to the setValue: if the condition fails. Try replacing with:

    [updateDevice setValue:[dict objectForKey:@"personality_company_master_values"] == [NSNull null] ? nil: [dict objectForKey:@"personality_company_master_values"] forKey:@"personality_company_master_values"];
    

    ie. pass the relevant element of the dictionary, not the dictionary itself.