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 9060651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:17:04+00:00 2026-06-16T15:17:04+00:00

Possible Duplicate: Does lastObject in NSMutableArray return a copy of the object? I’m following

  • 0

Possible Duplicate:
Does lastObject in NSMutableArray return a copy of the object?

I’m following Stanford course on iOS development and I have a doubt.
The thing is that I have an array (operandStack) with “operands” (NSNumbers) and I want a function that pops out the last element and then erase it from the array. This is what the tutorial sais (and it works):

- (double)popOperand
{
    NSNumber *operand = [self.operandStack lastObject];
    if(operand) [self.operandStack removeLastObject];
    return [operand doubleValue];
}

My question is: if with NSNumber *operand we’re just “pointing” to the last object and in the next line we remove this last object, how is it possible that we then return it? I mean, if we remove the last object and operand is just pointing to that object, then operand should be pointing to nothing, so I can’t understand how it’s possible that we can return operand…
I’d understand the code if operand was a copy of the last object of the array, but this is not the case…

Sorry for my english :S I hope I made myself clear.

Thanks,

Carlos

  • 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-06-16T15:17:05+00:00Added an answer on June 16, 2026 at 3:17 pm

    The removeLastObject method removes an object from the array, not from memory. Before you remove the object from the array, you assign it to a local variable. When compiling with ARC, local variables that point to objects have an implicit lifetime qualifier of __strong.

    Since you add the strong reference to the object before removing it from the array, your method temporarily takes ownership of it. Even if the method returned the object itself rather than its value, this would still work correctly, because ARC would insert an autorelease call instead of a release at the end of the method.

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

Sidebar

Related Questions

Possible Duplicate: Does C# have extension properties? I have the following: public static class
Possible Duplicate: Does Java guarantee that Object.getClass() == Object.getClass()? If I have a class
Possible Duplicate: Why does the background of TextView change it's size? I have a
Possible Duplicate: Does an abstact classes have a VTABLE? Does a vtable gets created
Possible Duplicate: Does SQL Server 2005 have an equivalent to MySql’s ENUM data type?
Possible Duplicate: Why does javascript replace only first instance when using replace? I have
Possible Duplicate: What does void(U::*)(void) mean? Considering the following: template <class T> class myButtoncb
Possible Duplicate: Does std::list::remove method call destructor of each removed element? Assume I have
Possible Duplicate: Why does C have a distinction between -> and . ? What
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a

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.