开发者

Is NSMutableArray recursive in releasing it's items?

开发者 https://www.devze.com 2023-02-02 16:29 出处:网络
If I have a 2 dimensional NSMutableArray eg, board = [[NSMutableArray alloc] initWithCapacity:boardHeight];

If I have a 2 dimensional NSMutableArray eg,

board = [[NSMutableArray alloc] initWithCapacity:boardHeight];
for (int y = 0; y < boardHeight; y++) {
    NSMutableArray *row = [[NSMutableArray alloc] initWithCapacity:boardWidth];

    for (int x = 0; x < boardWidth; x++) {
        [row insertObject:@"A string"];             
    }

    [board insertObject:row atIndex:y];
    [row release];
}

and I do

[board release];

Does that recursively release the array? Or do I have to manually go into the array and release each row?

If it does, and the object inserted into each row were a custom object, is there anything special I have to think about when w开发者_如何学运维riting the dealloc method for the custom object?


Everything will just work fine. The array will retain the objects when they are added and released when removed or the array is deallocated.

No extra work on that part.


When board is ready to be deallocated, it will send the release message to each object in itself. Even if those objects are NSArrays, they will still be released; and if those arrays are now ready to be deallocated, they will send release to their members, etc.

Your class should implement dealloc normally -- release any ivars that you hold a strong reference to before calling [super dealloc].

0

精彩评论

暂无评论...
验证码 换一张
取 消