NSArray initialization methods

久未见 提交于 2019-11-30 18:24:35

问题


What is the difference between initializing array with

NSArray * array = [NSArray array];

and

NSArray * array = @[];

回答1:


NSArray * array = @[]; is the new way of doing NSArray * array = [NSArray array];




回答2:


@[] is shorthand for:

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

Which is really just shorthand for [NSArray array], for all intents and purposes.

This is a feature added in a particular version of the compiler (and doesn't actually require runtime support for this particular syntax).

It is not at all like the @"" shorthand in that @"" produces a compile time constant and will cause no messaging at runtime. In fact, @"" (any @"sequence") is a special case in that it emits a compile time constant that is realized in the runtime with zero messaging; zero dynamism. A @"..." is more similar to an Objective-C class than it is to a regular instance of an object.



来源:https://stackoverflow.com/questions/14483531/nsarray-initialization-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!