Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why NSArray does not have a firstObject method? [closed]

But it does have a lastObject, anybody know why?

like image 860
zchenah Avatar asked Oct 25 '12 08:10

zchenah


People also ask

What is difference between NSArray and NSMutableArray?

NSArray creates static arrays, and NSMutableArray creates dynamic arrays.

Is NSArray ordered?

The answer is yes, the order of the elements of an array will be maintained - because an array is an ordered collection of items, just like a string is an ordered sequence of characters...


1 Answers

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!

like image 110
Mick MacCallum Avatar answered Nov 09 '22 21:11

Mick MacCallum