How to get the last object of an NSArray
So I have an array which the amount of items could vary. I was wondering if there is anyway I can get the last object of an
NSArray? I did think of having like an
int counter to trace the amount of items in the array however that seems too much of a hassle.
Does anyone know anyway that’s better than this?
4 Solutions Collect From Internet About “How to get the last object of an NSArray”
Expanding a bit on the question, there are several situations in which one could need the last object of an array, with different ways to obtain it.
In a straight Cocoa program
If one just wants to get the object in course of a standard Cocoa program, then this will do it:
To address the concern of the counter – no need to implement one’s own, either:
NSUInteger myCount = [myArray count];
Using key-value coding (KVC)
In case one needs to access the last object of an array through KVC, the story requires a bit of explanation.
First, requesting the value of a normal key from an array will create a new array consisting of the values of that key for each of the objects in the array. In other words, a request for the key
lastObject from an array will make the array send
valueForKey: to each of the objects using the key
lastObject on them. Apart from not being the intended result, it will also likely throw an exception.
So in case one really needs to send a key to the array itself (as opposed to its contents), the key needs to be prepended with an
@-sign. This tells the array that the key is intended for the array itself, and not its contents.
The key therefore has to have the form
@lastObject, and be used like this:
NSArray *arr = @[@1, @2, @3]; NSNumber *number = [arr valueForKey: @"@lastObject"];
In a sort descriptor
An example of how this key could be used in a real program is a situation where an array of arrays needs to be sorted by the last object in each of the inner arrays.
The above key is simply used in the sort descriptor:
NSArray *arrayOfArrays = @[@[@5, @7, @8], @[@2, @3, @4, @6], @[@2, @5]]; NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey: @"@lastObject" ascending: YES]; NSArray *sorted = [arrayOfArrays sortedArrayUsingDescriptors: @[sd]];
In a predicate
Likewise, in order to filter an array of arrays, the key can be used directly in a predicate:
NSPredicate *pred = [NSPredicate predicateWithFormat: @"self.@lastObject > 5"]; NSArray *filtered = [arrayOfArrays filteredArrayUsingPredicate: pred];
People preferring to leave the
self-part out can simply use the format
@"@lastObject > 5"
see the documentation
Maybe like this:
id object = [myArray lastObject];
A good tip is to option-click on any symbol you want to read documentation for. For example hold down the option key and click on any text that says
NSArray in your Xcode editor.
You get a nice popup with the basics, and a link for full detailed documentation.
Or if you want to get fancy:
- How can I create Xcode docsets that look and work like the ones in Apple Core Library
- How can I use a PKCS8 RSA DER Private Key in iOS?
- handleWatchKitExtensionRequest not responding to openParentApplication in Watchkit Extension (Swift)
- Presenting a view controller with transparency and animation
- Since Xcode 8 and iOS10, views are not sized properly on viewDidLayoutSubviews
- How can I make and connect a spreadsheet to my code using the Google Sheets API?
- App Store Submission: No Identities
- Check if user has valid auto-renewable subscription with Parse iOS SDK
- iOS Private API: lock device and power off the screen
- iAd in Sprite Kit Pauses the scene
- back button callback in navigationController in iOS
- Cannot debug app on Xcode 7.1 beta, with iOS 9.0 GM
- How to sort the table view from A-z?
- Unit Tests fail, then don't
- How to see plist variables result from Xcode