开发者

Difference between objectAtIndex and [0]

What is the difference between using the following:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *dir = [paths objectAtIndex:0]; 

    NSString *dir2 = paths[0开发者_C百科]; // this throws incompatible type exception


  1. paths is a pointer to instance of NSArray that you are sending the objectAtIndex: message. The receiver returns an id.
  2. paths[0] is the memory address of the beginning of an array in pure C. [] and NSArray are not the same thing.


Just FYI for all those out there that find this question now...

With LLVM, Objective-C supports object subscripting. So,

paths[0]

is equivalent to

[paths objectAtIndexedSubscript:0]

which is identical to

[paths objectAtIndex:0]

provided paths is an NSArray.

For more Objective-C literal syntax, see the docs here: http://clang.llvm.org/docs/ObjectiveCLiterals.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜