-4

I am new to Obejtive C so im looking at alot of sample code at the time and i noticed that people initialize their NSMutableArray differently.

NSMutableArray *items = [NSMutableArray array];

or

NSMutableArray *items = [[NSMutableArray alloc] init];

In both lines you end up with an NSMutableArray Object.
What is the difference between them or are they exactly the same?

1
  • the first one is factory method , which return a instance of nsmutablearray. in second one you are giving memory and retaining its count to 1. Commented Feb 11, 2014 at 18:41

1 Answer 1

6

The main difference between these is if you're not using ARC (Automatic Reference Counting). The first one returns a retained and autoreleased object. The second one returns an object that is only retained. So in the first case, you would want to retain it if you wanted to keep it around for longer than the current run loop. In the second case, you would want to release or autorelease it if you didn't want to keep it around.

Now that we have ARC, this changes things. Basically, in ARC code, it doesn't matter which of these you use.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.