我需要删除被添加的空对象
[mutArrSkills addObject:[NSNull null]];
我需要迭代吗?有没有函数可以从NSMutableArray中删除所有空值?
如果需要迭代,我该如何做?
我需要删除被添加的空对象
[mutArrSkills addObject:[NSNull null]];
我需要迭代吗?有没有函数可以从NSMutableArray中删除所有空值?
如果需要迭代,我该如何做?
removeObjectIdenticalTo:
方法,如下所示。[mutArrSkills removeObjectIdenticalTo:[NSNull null]];
去除空值。无需迭代。
[NSNull null] != [NSNull null]
... - Nekto [mutArrSkills addObject:[NSNull null]];
添加的元素将被该调用删除。 - IlanchezhianNSNull *nullValue = [NSNull null];
[mutArrSkills removeObjectIdenticalTo:nullValue];
希望这能有所帮助。
在数组中移除所有出现的指定对象。
讨论 该方法使用indexOfObjectIdenticalTo:方法来定位匹配项,然后使用removeObjectAtIndex:来移除它们。因此,匹配是使用对象地址确定的。如果数组不包含anObject,则该方法无效(尽管它会产生搜索内容的开销)。
[NSNull null] != [NSNull null]
... - NektoSwift
Array
转换为NSArray
,并使其可变,以便您可以删除Objective-C
遗留元素,然后再将其转换回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]
for(int i=0,i<[mutArrSkills count]; i++)
{
if([[mutArrSkills objectAtIndex:i] isKindOfClass:[NSNull Class]])
{
[mutArrSkills removeObjectAtIndex:i];
}
}