NSMutableArray是多维的,removeAllObjects?

时间:2015-01-17 21:23:58

标签: objective-c multidimensional-array nsarray automatic-ref-counting

ObjC w/ARC

在将removeAllObjects的新实例重新分配给它之前,是否需要在先前初始化的NSMutableArray(设置为多维,btw)上调用NSMutableArray

考虑:

@interface MyClass
@property(strong) NSMutableArray *myArray;
@end

@implementation MyClass

-(instancetype)init {

    if (self = [super init]) {
        [self initializeMyArrayWithData]  // once
        [self initializeMyArrayWithData]  // twice
    } 
    return self;
}

-(void)initializeMyArrayWithData {
    NSMutableArray *temp = [NSMutableArray arrayWithCapacity:8];
    for (int row = 0; row < 8; row++) {
        [temp addObject: [NSMutableArray arrayWithCapacity:8]];
        for (int col = 0; col < 8; col++) {
            [[temp objectAtIndex:row] insertObject:@"TEST" atIndex:col];
        }
    }

    // let's assume my array was previously initialized
    // with this method. Do I need to "clean-up" the previous
    // instance by removing all objects within it, or will
    // this be handled automatically by the property method using
    // the following simple assignment?

    self.MyArray = temp;
}
@end

1 个答案:

答案 0 :(得分:0)

不,你不必这样做。以下是发生的情况:ARC将告知NSMutableArrayself.MyArray引用的当前实例已释放。假设它是唯一的引用,数组将开始释放其内容,释放其每个对象。