내 "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]를 사용하면 배열에서 이전 객체를 제거하여 자동으로 뒤에서 객체를 해제합니다. 코인뷰가 다른 곳에서 개최된다면, 여전히 존재할 것입니다. 그러나 그 본성 상, 뭔가가 여전히 그것을 참조하고 있기 때문에 누설이 아닙니다. – CrimsonDiego