Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6367723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:35:08+00:00 2026-05-25T00:35:08+00:00

So I have an array which the amount of items could vary. I was

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:35:09+00:00Added an answer on May 25, 2026 at 12:35 am

    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:

    [myArray lastObject]
    

    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"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which contains around 50-200 hyperlinks. How can I pass this
i have an array which could look like this: $config[$name][$array]['key'] = 'value'; // here
I have an Array nested withing an Object that's inside an Object which is
I have an array which is a list of domains, I want to print
I have an array which contains another array Would I notate it this way?
I have an array which has the contents as the result of an sql
I have an array which i need to sort its elements by occurrence then
Lets say I have an array which has n dimensions. Now in order to
I have a string say string s =C:\\Data , I have an array which
Am I missing something in the Array documentation? I have an array which contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.