如何从数组中删除对象?

时间:2022-06-17 02:46:35

I was wondering how do you remove objects from an NSMutableArray, cause right now I use [astroids removeFromSuperview]; but it just gets rid of the image on screen but the instance itself is still present even when removed.

我想知道如何从NSMutableArray中删除对象,因为我现在使用[astroids removeFromSuperview];但它只是摆脱了屏幕上的图像,但即使被删除,实例本身仍然存在。

3 个解决方案

#1


-removeFromSuperview is a method of UIView, not NSMutableArray. It seems that the UIView instance you're calling that on is also kept in an MSMutableArray. You will need to use one of the NSMutableArray methods for removing objects on the array itself.

-removeFromSuperview是UIView的一种方法,而不是NSMutableArray。看来你正在调用它的UIView实例也保存在MSMutableArray中。您将需要使用NSMutableArray方法之一来删除阵列本身上的对象。

It is necessary to remove it in the array separately because it also owns the object in question, and has retained it independently of its use elsewhere.

有必要单独删除数组,因为它还拥有相关对象,并保留它独立于其他地方的使用。

NSMutableArray Methods for Removing Objects:

NSMutableArray删除对象的方法:

– removeAllObjects  
– removeLastObject  
– removeObject:  
– removeObject:inRange:  
– removeObjectAtIndex:  
– removeObjectsAtIndexes:  
– removeObjectIdenticalTo:  
– removeObjectIdenticalTo:inRange:  
– removeObjectsFromIndices:numIndices:  
– removeObjectsInArray:  
– removeObjectsInRange:

See http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nsmutablearray_Class/index.html

#2


NSMutableArray contains a removeObjectAtIndex message.
NSMutableArray documentation

NSMutableArray包含removeObjectAtIndex消息。 NSMutableArray文档

#3


Supposing you are removing a view from its superview, then the removeFromSuperview does not send a release message to your view, that would cause it to be immediately freed (if no other objects did retain it).

假设您要从其超级视图中删除视图,则removeFromSuperview不会向您的视图发送释放消息,这将导致它立即被释放(如果没有其他对象确实保留它)。

Instead it sends an autorelease message. This causes the view to actually get removed from memory at the end of the run loop, not immediately after your removeFromSuperview message.

相反,它发送自动释放消息。这会导致视图在运行循环结束时实际从内存中删除,而不是在removeFromSuperview消息之后立即删除。

#1


-removeFromSuperview is a method of UIView, not NSMutableArray. It seems that the UIView instance you're calling that on is also kept in an MSMutableArray. You will need to use one of the NSMutableArray methods for removing objects on the array itself.

-removeFromSuperview是UIView的一种方法,而不是NSMutableArray。看来你正在调用它的UIView实例也保存在MSMutableArray中。您将需要使用NSMutableArray方法之一来删除阵列本身上的对象。

It is necessary to remove it in the array separately because it also owns the object in question, and has retained it independently of its use elsewhere.

有必要单独删除数组,因为它还拥有相关对象,并保留它独立于其他地方的使用。

NSMutableArray Methods for Removing Objects:

NSMutableArray删除对象的方法:

– removeAllObjects  
– removeLastObject  
– removeObject:  
– removeObject:inRange:  
– removeObjectAtIndex:  
– removeObjectsAtIndexes:  
– removeObjectIdenticalTo:  
– removeObjectIdenticalTo:inRange:  
– removeObjectsFromIndices:numIndices:  
– removeObjectsInArray:  
– removeObjectsInRange:

See http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nsmutablearray_Class/index.html

#2


NSMutableArray contains a removeObjectAtIndex message.
NSMutableArray documentation

NSMutableArray包含removeObjectAtIndex消息。 NSMutableArray文档

#3


Supposing you are removing a view from its superview, then the removeFromSuperview does not send a release message to your view, that would cause it to be immediately freed (if no other objects did retain it).

假设您要从其超级视图中删除视图,则removeFromSuperview不会向您的视图发送释放消息,这将导致它立即被释放(如果没有其他对象确实保留它)。

Instead it sends an autorelease message. This causes the view to actually get removed from memory at the end of the run loop, not immediately after your removeFromSuperview message.

相反,它发送自动释放消息。这会导致视图在运行循环结束时实际从内存中删除,而不是在removeFromSuperview消息之后立即删除。