在我的“connect4”风格的游戏中,我有一个代表7x6网格的数组,数组中的每个“单元格”都包含NSNull或UIView子类'CoinView'。以下是从NSMutableArray和主视图中删除对象的正确方法吗?正确地从视图和数组中删除对象?
- (IBAction)debugOrigin:(id)sender {
int x = 0;
int y = 0;
//get the coin object form the grid
CoinView *coin = [[grid objectAtIndex:x] objectAtIndex:y];
//cancel if there's no coin there
if ([coin isKindOfClass:[NSNull class]]) { return; }
//remove the coin from memory
[coin removeFromSuperview];
coin = nil;
[[grid objectAtIndex:x] setObject:[NSNull null] atIndex:y]; //will this leak?
}
谢谢!
如果你使用ARC,这应该没问题。使用[array setObject:atIndex]将从数组中移除任何先前的对象,并在幕后自动释放它。如果CoinView被保存到其他任何地方,它仍然存在 - 但由于这种性质,它不是泄漏,因为某些东西仍然会引用它 – CrimsonDiego