启用 ARC 的 Objective-c 中的以下内容是否合法?
NSMutableArray * smallArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
smallArray = [[NSMutableArray alloc] initWithObjects:@"4", "5", nil];
我认为应该是。但是,在这种情况下,它在第四行给了我 EXEC_BAD_ACCESS:
NSMutableArray * bigArray = [[NSMutableArray alloc] init];
NSMutableArray * smallArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
[bigArray addObject: smallArray];
smallArray = [[NSMutableArray alloc] initWithObjects:@"4", "5", nil];
addObject 复制指针对吗?因此,如果我将新的内存段分配给 smallArray 以指向,那有什么问题?
但是,此代码段不会崩溃:
NSMutableArray * bigArray = [[NSMutableArray alloc] init];
NSMutableArray * smallArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
[bigArray addObject: smallArray];
smallArray = [[NSMutableArray alloc] init];
[smallArray addObject:@"4"];
[smallArray addObject:@"5"];
这里发生了什么?