views:

410

answers:

3

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.

+1  A: 

NSMutableArray contains a removeObjectAtIndex message.
NSMutableArray documentation

toast
Also `removeObject:`, if all you have is the object and you don't want to look for its first index yourself.
Peter Hosey
+10  A: 

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

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:
– 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

Sean Murphy
A: 

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

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.

IlDan