3

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"];
Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38
vicky
  • 253
  • 2
  • 14

3 Answers3

1

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.

pbasdf
  • 21,386
  • 4
  • 43
  • 75
0

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 ]?

Mikhail Grebionkin
  • 3,806
  • 2
  • 17
  • 18
  • I have set the brackets but it giving me the same error. setting up values here [updateDevice setValue:[dict objectForKey:@"clip_name"] forKey:@"clip_name"]; this is correct but for null values i am not getting hoe to check for it. suggest me other way to check for null value. @Mikhail – vicky Jul 04 '16 at 11:14
  • @vicky check my answer – balkaran singh Jul 04 '16 at 11:20
0

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"];
balkaran singh
  • 2,754
  • 1
  • 17
  • 32
  • no it is not setting proper value for personality_company_master_values it is giving null but it has some value for some clips @balkaran singh – vicky Jul 04 '16 at 11:42
  • it check's personality_company_master_values has value and personality_company_master_values is not null class. if personality_company_master_values have value it set the value. – balkaran singh Jul 04 '16 at 11:45
  • then why it's not setting the value i am using the code you have suggested. @balkaran Singh – vicky Jul 04 '16 at 11:47
  • check your db personality_company_master_values have any value or not. you can usr nslog("%@",[updateDevice valueForKey:@"personality_company_master_values"]); – balkaran singh Jul 04 '16 at 11:48
  • I can't say about that...i am in trouble here... but it's not working, anyway thanks for the help dude @balkaran Singh – vicky Jul 04 '16 at 11:54