-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?

www40
  • 285
  • 1
  • 7
  • 19
  • 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. – Pawan Rai Feb 11 '14 at 18:41

1 Answers1

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.

Gavin
  • 8,204
  • 3
  • 32
  • 42