0

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?

1
  • Not very clear about this, but I happened to see initWithObjects:@"4", "5", nil]... have you lost @ before "5"?? Commented Jul 18, 2012 at 14:20

2 Answers 2

2

Try this:

smallArray = [[NSMutableArray alloc] initWithObjects:@"4", "5", nil];

The second object must be @"5", not just "5".

Sign up to request clarification or add additional context in comments.

Comments

0

I guess this is because you forgot the @ in the string "5". it should be:

smallArray = [[NSMutableArray alloc] initWithObjects:@"4", @"5", nil];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.