1

我正在尝试创建一个游戏并且遇到了一个可能很容易解决的问题。

随着玩家进行游戏,许多对象(车辆)将被添加和移除。

车辆被添加到一个名为 currentVehiclesMutableArray 的数组中。

我的问题是我无法弄清楚如何保留 Vehicle 以便它保留在数组中,直到我完成它。

4

1 回答 1

4

NSMutableArray 会自动保留您添加到其中的任何内容。

事实上,您必须确保在添加对象后释放它,否则您将发生内存泄漏。

例如:

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
于 2010-06-10T20:28:56.153 回答