0

From my JSON feed i create a NSMutableArray to hold my NDDictionary objects. When I come to display this in a UITableView it crashes due to <null> Values. So I need to know, How exactly can i remove these "<null>" Objects from my Array.

Here is a print out of what is returned from the JSON:

Service Names: (
"Nether Edge to Woodhouse",
"Barnsley to Rotherham",
"Woodhouse to Nether Edge",
"Rotherham to Barnsley",
"Doncaster to Worksop",
"Worksop to Doncaster",
"Penistone to Barnsley Interchange",
"Barnsley Interchange to Penistone",
"<null>",
"Barnsley to Rotherham",
"Rotherham to Barnsley",
"<null>",
"Buttershaw to St Bedes RC Upper School",
"St Bedes RC Upper School to Buttershaw",
"<null>",
"Sandholme Drive to Ilkley GS",
"Ilkley GS to Sandholme Drive",
"<null>",
"Barnsley to Rotherham",
"Rotherham to Barnsley"
)

I have tried things such as :

[serviceNames removeObject:@"<null>"];
[serviceNames removeObject:[NSNull null]];

and

[serviceNames removeObjectsIdenticalTo:@"<null>"];
[serviceNames removeObjectsIdenticalTo:[NSNull Null]];

All which have failed up to now so can anybody please tell me a simple way of removing the "<null>"objects from this array as its driving me mad!

Wain
  • 118,658
  • 15
  • 128
  • 151
Damien D
  • 19
  • 9
  • 1
    Loop over the array, log each item and its class name. – Wain Nov 27 '13 at 18:35
  • Check for Null when you create cell for your table. – Eugene P Nov 27 '13 at 18:37
  • Try [arr removeObject:@"\"\""]; – Grzegorz Krukowski Nov 27 '13 at 18:41
  • 1
    `serviceNames` isn't really an `NSMutableArray`; from your own logs I can see this is the case `-[__NSArrayI removeObjectIdenticalTo:]: unrecognized selector sent to instance 0x8bb4370`. that is why `[serviceNames removeObjectsIdenticalTo:[NSNull Null]];` isn't working. – Brad Allred Nov 27 '13 at 20:02
  • Someone already suggested using a predicate so I will just leave this here and upvote their answer. NSArray *nonNullResults = [serviceNames filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF != null"]] – Eric Mentele Mar 16 '17 at 00:11

5 Answers5

1

you may use a NSPredicate :

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF CONTAINS %@)", @"<null>"];
    [serviceNames filterUsingPredicate:predicate];

With a for-in loop over the serviceNames, you should notice all the null strings are gone.

0

Try this,
Note: this I not tested.

tempArray is your real array.

NSMutableArray *mainArray = [[NSMutableArray alloc]init];
for (int i = 0; i < [tempArray count]; i++) {
    id obj = [tempArray objectAtIndex:i];
    if (![obj  isKindOfClass:[NSNull class]]) { // or if (![obj  isKindOfClass:[[NSNull null]class]]) {
        [mainArray addObject:obj];
    }
}

NSLog(@"%@",mainArray);
Lance
  • 8,872
  • 2
  • 36
  • 47
Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29
0

You can remove all Null from array in this way

for(uint i=0;i<serviceNames.count;i++)
{
    if([[serviceNames objectAtIndex:i] isKindOfClass:[NSNull class]])
        [serviceNames removeObjectAtIndex:i];
}

then reload your table

Anand Suthar
  • 3,678
  • 2
  • 30
  • 52
0

here is a shorter and possibly more efficient way to do it:

Edit: the answer to this duplicate question is even better than my own.

simply use [mutArrSkills removeObjectIdenticalTo:[NSNull null]];

if your array is not an NSMutableArray then get a mutable copy like so NSMutableArray* mutableArray = [array mutableCopy]; This is why when you tried this solution it didn't work; because you are trying to mutate an immutable array.

[NSNull null] produces a singleton object so you can just compare equality of address if you like. It an unnecessary check and slow down to either use isKindOfClass: or isEqual:. If you are going to iterate the objects yourself just use the == operator instead of sending costly messages.

Community
  • 1
  • 1
Brad Allred
  • 7,323
  • 1
  • 30
  • 49
  • When using the removeObjectIdenticalTo:[NSNull null]]; i get the following: http://pastie.org/private/oudhurkymn6vszdmmfalcq Thanks – Damien D Nov 27 '13 at 19:52
  • @DamienD that is because you are sending that message to an _immutable_ array. you need a mutable array (I updated my answer to show you). – Brad Allred Nov 27 '13 at 19:56
  • I am calling it on a NSMutableArray :/ tempServiceNames = [[NSMutableArray alloc] init]; – Damien D Nov 27 '13 at 21:02
  • @DamienD show me your code, because you wouldn't be getting that error if you were really sending that message to a mutable array. you are doing something wrong. – Brad Allred Nov 27 '13 at 21:04
  • header file is at the top, and the implementation is below, http://pastie.org/private/fk2gw29skho8wshi5ss0a Thanks. – Damien D Nov 27 '13 at 21:15
  • @DamienD you are clobbering your mutable array on line 126 `tempServiceNames = [jsonDict valueForKey:@"desc"];` that is assigning a new immutable array to `tempServiceNames`. If you want mutable containters returned by `NSJSONSerialization` then you must pass `NSJSONReadingMutableContainers` as an option. I question your design here; your initialization of all your temporary objects is meaningless and they probably don't belong in your interface :/ – Brad Allred Nov 27 '13 at 21:23
  • Tempoary objects where created for one of the above answers to test. I originally only used one set of arrays for each. I shall attept to add the option and see if this resolves the issue. – Damien D Nov 27 '13 at 21:31
  • @DamienD even then you still have a bunch of useless `[[NSMutableArray alloc] init]` lines. there is no point in allocating a new array only to overwrite it later. – Brad Allred Nov 27 '13 at 21:37
  • Okay, so how would you recommend doing this? I apologise for my mistakes, I am still quite new to C#. – Damien D Nov 27 '13 at 21:41
  • @DamienD you need more reputation before we can chat :( this isn't C# BTW... [Code Review](http://codereview.stackexchange.com/) is probably a better forum for getting advice on improving code. – Brad Allred Nov 27 '13 at 21:45
  • :/ thats a shame, could do with some support from somebody who knows about this as im Java self taught. Is there no way outside of here we can chat or is that against T&C's. – Damien D Nov 27 '13 at 21:49
0

My solution was to create a category on NSNull that responded to the most common messages sent to other JSON objects (integerValue, etc). It returned whatever was most appropriate for the message. Worked like a charm.

David H
  • 40,852
  • 12
  • 92
  • 138