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
paths
is a pointer to instance ofNSArray
that you are sending theobjectAtIndex:
message. The receiver returns anid
.paths[0]
is the memory address of the beginning of an array in pure C.[]
andNSArray
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
精彩评论