0

I am receiving JSON response from server, most of the values are NULL, I have written these lines of code, assuming that it should work fine, but somehow it is giving me again the same Null error, Kindly review my question and help me out..

 NSMutableArray * s = [data valueForKey:@"my_images"];
         NSLog(@"array is  %@",s);



         for (int i=0; i < [s count]; i++)
         {

             if (s[i] !=[NSNull null])
             {
                 NSLog(@"%@",s[i]);

             }

             else
             {

                 NSLog(@"%@",s[i]);

             }

         }


I need to get split images on that location instead of merged values, My output is here..

 array is  (
"mobile2.jpg,mobile1.jpg,mobile.jpg",
"artificial.jpg,grass.jpg",
"11.JPG,chair.JPG",
"oc.jpg",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>",
"<null>"

)

Majid Bashir
  • 558
  • 1
  • 4
  • 27
  • 2
    Your `if` / `else` statements give the same output, don't they? Also, if you're checking for `null` but the fields are `""` then they aren't `null`, they're strings. Sorry if I'm not understanding your question. As for splitting the first three strings, you can use `componentsSeparatedByString` and check for commas. – Tim Nov 24 '15 at 01:28
  • 1
    you are searching value for key `my_images`. What you have given as output is not valid JSON. It is just an array of strings. Can you post your JSON to the question. – PK20 Nov 24 '15 at 01:29
  • yes it is.. it goes to to the end of mutable array, but at the end it is crashing – Majid Bashir Nov 24 '15 at 01:30
  • Please paste the full output. We need to see the value you get from `[data valueForKey:@"my_images"];` or the full json output. – Jess Nov 24 '15 at 01:30
  • see updated.. full values – Majid Bashir Nov 24 '15 at 01:32
  • I've already voted this as 'Unclear what you're asking', because this seems like it's probably an XY problem, because there's no clear error statement--no actual error message, no explanation of what you're ***actually*** trying to do... and the provided answers are only explaining how to remove `[NSNull null]` from a mutable array. If that's the actual question, this question is a duplicate [of this existing question which already has plenty of answers](http://stackoverflow.com/q/9192301/2792531). – nhgrif Nov 24 '15 at 03:17

3 Answers3

1

You need to check the value of object is not null before add it to the array.

NSArray* s = [data valueForKey:@"my_images"];
NSMutableArray* data = [NSMutableArray array];
for (id object in s) {
   if ([object isKindOfClass:[NSNull class]] == NO) {
       [data addObject:object];
   }
}
hiennv92
  • 810
  • 1
  • 8
  • 12
  • 1
    There's a much easier way, given we can just pull the objects from the dictionary as a mutable array: http://stackoverflow.com/q/9192301/2792531 – nhgrif Nov 24 '15 at 03:20
0

Check NUll Data.. Hope this is HelpFull.

 if ([str isKindOfClass:[NSArray class]]){
    return str;
}
else
{
    str = [str isEqual:[NSNull null]]?@"":[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([str isKindOfClass:[NSNull class]] || [str isEqual:[NSNull null]] || [str isEqualToString:@"<null>"]  || [str isEqualToString:@"(null)"] || [str isEqualToString:@""] || [str isEqualToString:@"0"])
    {
        return @"";
    }
Nil Rathod
  • 450
  • 5
  • 21
-1

u can try this

for (NSString *item in images){
    if (item && ![item isEqual:[NSNull null]]){
        // do something
    }
}
Vian
  • 1
  • 2
  • It'd be impossible for `item` to be `nil` in a for-in loop. You can't put `nil` in an array, and `nil` serves as the terminator for the loop, so even if you could manage `nil` in any of the indexes, it'd simply serve as a signal that the loop made it to the end and wouldn't enter the body with that argument. – nhgrif Nov 24 '15 at 03:08