Question

I am trying to create a game and have run into what is probably a pretty easy problem to solve.

As the player goes through the game, many objects (Vehicle) will be added and removed.

The Vehicles get added to an array called currentVehiclesMutableArray.

My problem is I cant figure out how to retain a Vehicle so that it remains in the array until I am finished with it.

Was it helpful?

Solution

A NSMutableArray automatically retains anything you add to it.

In fact, you have to make sure you release an object after you added it, or you'll have a memory leak.

For example:

Vehicle *vehicle = [[Vehicle alloc] init];

[mutableArray addObject:vehicle];

[vehicle release]; // you should release it, because it was retained by the array

// at this point, mutableArray holds your vehicle object, and is retaining it
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top