Search code examples
objective-cnsarrayobjective-c-literals

Difference between @[] and [NSArray arrayWithObjects:]


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?


Solution

  • 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.