But it does have a lastObject, anybody know why?
NSArray creates static arrays, and NSMutableArray creates dynamic arrays.
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...
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With