如何从数组中删除对象?

时间:2009-07-21 00:51:25

标签: iphone objective-c cocoa

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

3 个答案:

答案 0 :(得分:14)

-removeFromSuperviewUIView的方法,而非NSMutableArray。您正在调用的UIView实例似乎也保存在MSMutableArray中。您需要使用NSMutableArray方法之一来删除阵列本身上的对象。

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

NSMutableArray删除对象的方法:

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

请参阅http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nsmutablearray_Class/index.html

答案 1 :(得分:1)

NSMutableArray包含removeObjectAtIndex条消息 NSMutableArray documentation

答案 2 :(得分:0)

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

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

相关问题