如何从NSMutableArray中删除所有对象?

时间:2022-06-01 21:06:02

I need to remove all objects from an NSMutableArray. I can't seem to do this by enumerating as the code crashes.

我需要从NSMutableArray中删除所有对象。我似乎无法通过枚举代码崩溃来做到这一点。

Can anyone tell me the best way to do this with a code example if possible?

有人可以告诉我使用代码示例执行此操作的最佳方法吗?

4 个解决方案

#1


31  

This should do the trick:

这应该是诀窍:

[myArray removeAllObjects];

#2


0  

In Swift 3:

在Swift 3中:

yourArry.removeAllObjects()

#3


0  

  // In Swift     

 arrayName.removeAllObjects()

#4


-2  

in case [YourArray removeAllObjects]; doesn't work.

以防[YourArray removeAllObjects];不起作用。

Then do it manually as below:

然后手动执行以下操作:

int c = (int)[YourArray count]-1;
for (int l = 0; l <= c; l++) {[YourArray removeObjectAtIndex:0];}

#1


31  

This should do the trick:

这应该是诀窍:

[myArray removeAllObjects];

#2


0  

In Swift 3:

在Swift 3中:

yourArry.removeAllObjects()

#3


0  

  // In Swift     

 arrayName.removeAllObjects()

#4


-2  

in case [YourArray removeAllObjects]; doesn't work.

以防[YourArray removeAllObjects];不起作用。

Then do it manually as below:

然后手动执行以下操作:

int c = (int)[YourArray count]-1;
for (int l = 0; l <= c; l++) {[YourArray removeObjectAtIndex:0];}