Search code examples
objective-cnsarray

Objective-C NSArray Access


It seems that in Objective-C there are two ways to access an NSArray. I could access the 'i'th element of NSArray 'array' by:

array[i]

or

[array objectAtindex:i]

Is there any functional difference between these two options?


Solution

  • There is no a slight difference.

    The shorter version uses objectAtIndexedSubscript: as opposed to objectAtIndex:. These methods are identical in their effect, but you can implement objectAtIndexedSubscript: in your own classes to allow element access with the [index] syntax.

    The shorter version is probably an instance of the "modern Objective-C syntax".

    See this link for some more information on the modern syntax and other changes.

    See this other link for information on Objective-C literals, courtesy of @luk2302.