0

Which of these, if either, is more efficient?

NSMutableArray *array = [NSMutableArray arrayWithArray:@[@1, @2]];

or

NSMutableArray *array = [@[@1, @2] mutableCopy];

Or are these the same internally?

1
  • Are they the same internally? Commented Jun 6, 2015 at 13:30

2 Answers 2

4

The two options you provide both first create an NSArray and then create an NSMutableArray from the NSArray so there is essentially no difference.

There is a third option that would be ever so slightly better in this case:

NSMutableArray *array = [NSMutableArray arrayWithObjects:@1, @2, nil];

This doesn't create an intermediate NSArray like your other two options.

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

Comments

0

While using ARC there is no difference in your example. But if you would use NSArray variable instead of static @[@1, @2], in case of this array being nil the first would return you empty array and the second one will return you 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.