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

一个人想着一个人 提交于 2019-11-27 05:16:41

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

[mutArrSkills removeObjectIdenticalTo:[NSNull null]];

to remove the null values. No need to iterate.

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).

You can try doing this,

NSNull *nullValue = [NSNull null]; 

[mutArrSkills removeObjectIdenticalTo:nullValue];

I hope this helps.

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]

You may iterate like this.

for(int i=0,i<[mutArrSkills count]; i++)
{
  if([[mutArrSkills objectAtIndex:i] isKindOfClass:[NSNull Class]])
    {
    [mutArrSkills removeObjectAtIndex:i];  
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!