Is the following legal in Objective-c with ARC enabled?
NSMutableArray * smallArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
smallArray = [[NSMutableArray alloc] initWithObjects:@"4", "5", nil];
I thought it should be. However, in this situation it gives me EXEC_BAD_ACCESS on the forth line:
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 copies the pointer right? So if I allocate a new segment of memory to smallArray to point to, what is wrong with that?
However this code segment does not crash:
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"];
What's going on here?
Try this:
smallArray = [[NSMutableArray alloc] initWithObjects:@"4", "5", nil];
The second object must be @"5" , not just "5" .
I guess this is because you forgot the @ in the string "5". it should be:
smallArray = [[NSMutableArray alloc] initWithObjects:@"4", @"5", nil];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.