NSArray initialization methods

What is the difference between initializing an array with

NSArray * array = [NSArray array];

      

and

NSArray * array = @[];

      

+3


source to share


2 answers


NSArray * array = @[];

is a new way to do NSArray * array = [NSArray array];



+7


source


@[]

is shorthand for:

id a = nil;
NSArray* array = [NSArray arrayWithObjects:&a count:0];

      

It is really just a shorthand for [NSArray array]

for all intents and purposes.



This is a feature added to a specific version of the compiler (and doesn't really require runtime support for that specific syntax).

This is not at all like shorthand @""

in that it @""

creates a compile-time constant and does not result in run-time messaging. In fact, @""

(any @"sequence"

) is a special case in that it emits a compile-time constant that is implemented at runtime with zero messaging; zero dynamism. A @"..."

looks more like an Objective-C class than a regular object instance.

+9


source







All Articles