Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between @[] and [NSArray arrayWithObjects:] [duplicate]

Possible Duplicate:
Should I prefer to use literal syntax or constructors for creating dictionaries and arrays?

Is there any difference between:

NSArray *array = @[@"foo", @"bar"];

and

NSArray *array = [NSArray arrayWithObjects:@"foo", @"bar", nil];

Is one of those more stable, faster, or anything else?

like image 246
Almas Adilbek Avatar asked Oct 11 '12 07:10

Almas Adilbek


People also ask

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?

An object representing a static ordered collection, for use instead of an Array constant in cases that require reference semantics.

Is NSArray ordered?

The answer is yes, the order of the elements of an array will be maintained - because an array is an ordered collection of items, just like a string is an ordered sequence of characters...

Can NSArray contain nil?

arrays can't contain nil. There is a special object, NSNull ( [NSNull null] ), that serves as a placeholder for nil.


1 Answers

This documentation doesn't mention anything about efficiency directly, but does mention that

NSArray *array = @[@"foo", @"bar"];

is equivalent to

NSString *strings[3];
strings[0] = @"foo";
strings[1] = @"bar";
NSArray *array = [NSArray arrayWithObjects:strings count:2];

I would have to assume that at an assembly level, the two are identical.

Thus the only difference is preference. I prefer the former, it's faster to type and more direct to understand.

like image 132
James Webster Avatar answered Sep 17 '22 15:09

James Webster