Why NSArray does not have a firstObject method?

ios, objective-c

Solution

My guess is because `lastObject` reduces more boilerplate code. You use `[array lastObject]` to replace either `[array objectAtIndex:array.count - 1]` or `array[array.count - 1]` using modern Objective-C syntax.

Whereas in the case of `firstObject` you can simply check `[array objectAtIndex:0]` or `array[0]`. It just helps streamline things to be able to call `lastObject` instead of typing out that function.

Update

As @Nathaniel Symer suggested in his comment above, `firstObject` has previously been available but only in private API (I believe since iOS 4). However, as of the release of the iOS 7 SDK, `firstObject` is now publicly available!

Problem

But it does have a lastObject, anybody know why?

Original source