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

1.5k views Asked by At

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"];
3

There are 3 answers

0
pbasdf On BEST ANSWER

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.

2
Mikhail Grebionkin On

You have some errors in your code. First of all it is better to use isEqual: method rather then == to compare with NSNull:

[[dict objectForKey:@"personality_company_master_values"] isEqual:[NSNull null]]

One more issue is how you are using ?: operator. If set brackets it will looks like:

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

Are you sure dict isn't nil? Or maybe you've missed ]?

5
balkaran singh On

plz use this code before set the value

 if ([updateDevice valueForKey:@"personality_company_master_values"] && ![[updateDevice valueForKey:@"personality_company_master_values"] isKindOfClass:[NSNull class]])
 [updateDevice setValue:[dict objectForKey:@"personality_company_master_values"]    forKey:@"personality_company_master_values"];