How will I be able to remove [NSNull Null] objects from NSMutableArray?
Asked Answered
I

5

13

I need to remove Null object added by

 [mutArrSkills addObject:[NSNull null]];

Do I need to iterate? Is there any function to remove all null values from NSMutableArray?

If need to Iterate, how will I do that?

Illusionism answered 8/2, 2012 at 11:19 Comment(1)
the removeObjectIdenticalTo also gonna itrate the array so why don't you itrate your self.Dairen
S
31

You can use NSMutableArray's removeObjectIdenticalTo: method, as follows

[mutArrSkills removeObjectIdenticalTo:[NSNull null]];

to remove the null values. No need to iterate.

Specs answered 8/2, 2012 at 11:24 Comment(3)
Aure you sure? I'm not because Objects are considered identical if their object addresses are the same.. AS you should know, [NSNull null] != [NSNull null]...Rhody
Yes, as far as concerned in this case, the elements that are added by ` [mutArrSkills addObject:[NSNull null]];` are removed with that call.Specs
AS you should know, [NSNull null] is indeed equal to [NSNull null]. There is only one instance ever of class NSNull. All objects of that class are the same pointer.Translunar
M
1

removeObjectIdenticalTo:

Removes all occurrences of a given object in the array.

Discussion This method uses the indexOfObjectIdenticalTo: method to locate matches and then removes them by using removeObjectAtIndex:. Thus, matches are determined using object addresses. If the array does not contain anObject, the method has no effect (although it does incur the overhead of searching the contents).

Mukund answered 8/2, 2012 at 11:25 Comment(2)
Aure you sure? I'm not because Objects are considered identical if their object addresses are the same.. AS you should know, [NSNull null] != [NSNull null]...Rhody
@Rhody pls refer my answer down.Bucko
B
1

You can try doing this,

NSNull *nullValue = [NSNull null]; 

[mutArrSkills removeObjectIdenticalTo:nullValue];

I hope this helps.

Bordereau answered 8/2, 2012 at 11:51 Comment(0)
M
0

In Swift you first have to cast your Swift Array to NSArray, make and make it mutable so you can remove the Objective-C leftover elements, then cast it back to Array.

Fatal error: NSArray element failed to match the Swift Array Element type

// my crashing array, containing a not String element, like NSNull or anything else
let myUnsafeSwiftArray: [String]

// make it safely NSArray, then make it mutable
let mutableUnsafeArray = NSMutableArray(array: myUnsafeSwiftArray as NSArray)

// remove leftover class, like [NSNull null] aka NSNull.init()
unsafeTextures.removeObject(identicalTo: NSNull.init())

// Cast the safe array back to its supposed to by element type
let safeArray = unsafeTextures as? [String]
Milliken answered 20/3, 2019 at 15:0 Comment(0)
R
-1

You may iterate like this.

for(int i=0,i<[mutArrSkills count]; i++)
{
  if([[mutArrSkills objectAtIndex:i] isKindOfClass:[NSNull Class]])
    {
    [mutArrSkills removeObjectAtIndex:i];  
   }
}
Rachealrachel answered 8/2, 2012 at 11:25 Comment(1)
That doesn't work at all if you have two successive NSNull objects in the array.Translunar

© 2022 - 2024 — McMap. All rights reserved.