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?
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.
An object representing a static ordered collection, for use instead of an Array constant in cases that require reference semantics.
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...
arrays can't contain nil. There is a special object, NSNull ( [NSNull null] ), that serves as a placeholder for nil.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With