Difference between @[] and [NSArray arrayWithObjects:]

Almas Adilbek picture Almas Adilbek · Oct 11, 2012 · Viewed 10.4k times · Source

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?

Answer

James Webster picture James Webster · Oct 11, 2012

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.