Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between NSArray.array/.new /@[]/alloc-init

There seem to be different methods of instantiating NSArrays (same for NSDictionary and some others).

I know:

  1. [NSArray array]
  2. [NSArray new]
  3. @[]
  4. [[NSArray alloc] init]

For readability reasons I usually stick with [NSArray array], but what is the difference between all those, do they all really do the same?

like image 847
MarkHim Avatar asked Oct 23 '15 07:10

MarkHim


People also ask

What is the difference between NSArray and array in Swift?

Array is a struct, therefore it is a value type in Swift. NSArray is an immutable Objective C class, therefore it is a reference type in Swift and it is bridged to Array<AnyObject> . NSMutableArray is the mutable subclass of NSArray . Because foo changes the local value of a and bar changes the reference.

What's a difference between NSArray and NSSet?

The main difference is that NSArray is for an ordered collection and NSSet is for an unordered collection. There are several articles out there that talk about the difference in speed between the two, like this one. If you're iterating through an unordered collection, NSSet is great.

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

What is an NSArray?

NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects. NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArrayRef . See Toll-Free Bridging for more information on toll-free bridging.


2 Answers

Results are same for all of them, you get a new empty immutable array. These methods have different memory management implications though. ARC makes no difference in the end, but before ARC you would have to use the right version or send appropriate retain, release or autorelease messages.

  • [NSArray new], [[NSArray alloc] init] return an array with retain count is 1. Before ARC you would have to release or autorelease that array or you'd leak memory.

  • [NSArray array], @[] return an already autoreleased array (retain count is 0). If you want it to stick around without ARC you'd have to manually retain it or it would be deallocated when the current autorelease pool gets popped.

like image 101
Sven Avatar answered Oct 11 '22 08:10

Sven


  1. [NSArray array] : Create and return an empty array

  2. [NSArray new] : alloc, init and return a NSArray object

  3. @[] : Same as 1.

  4. [[NSArray alloc] init] : Same as 2.

Different between [NSArray array] and [[NSArray alloc] init] is if there are non-ARC:

  • [NSArray array] is an autorelease object. You have to call retain if you want to keep it. E.g when you return an array.

  • [[NSArray alloc] init] is an retained object. So you don't have to call retain more if you want keep it.

With ARC, they are same.

like image 8
tuledev Avatar answered Oct 11 '22 07:10

tuledev