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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:45:53+00:00 2026-06-11T09:45:53+00:00

In my app I have a method that, when I press a button, adds

  • 0

In my app I have a method that, when I press a button, adds a string to a NSMutableArray which is the model for a UITableView.

 - (void)addPressed:(id)sender
{
    NSString *string = @"aString";
    [self.array addObject:string];
    NSLog(@"Array count: %d",[self.array count]);
    [self.tableView reloadData];
}

Problem is that the adding works the first time only if I press twice the button connected to this action I get this output:

2012-09-16 21:33:08.766 iUni[3066:c07] Array count: 1 //Which is fine since it worked
2012-09-16 21:33:08.952 iUni[3066:c07] Array count: 1 //Now count should be 2!!

Anyone has a guess on why is this happening?

I added the @property, synthesized it and lazy instatiated it this way:

- (NSMutableArray *)array
{
    if (!_array) {
        NSMutableArray *array = [NSMutableArray array];
        _array = array;
    }
    return _array;
}
  • 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-11T09:45:54+00:00Added an answer on June 11, 2026 at 9:45 am

    Your array is being created as an unowned (autoreleased, actually) object, which means that it is destroyed shortly after each time your accessor is called. It is then recreated the next time you access it, which gives you a new, empty array.

    You need to create an owned version of the array to store into your instance variable:

    if (!_array) {
        _array = [[NSMutableArray alloc] init];
        // Note no need to create a temp variable.
    }
    return _array;
    

    You could also turn on ARC, which would have taken care of this for you and is a good idea anyways.

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

Sidebar

Related Questions

I have a very basic doubt regarding the method that gets executed when app
I have this method in App Delegate that makes a window and content view
i have an app that on the init method , i need to load
I have the method below, which in my Blackjack app will get the value
I have a Winforms App (.NET 3.X) That runs a method in a class
I have an update method that is called every frame. Each update, it adds
I have an app with several UIWebView's and when I press the button to
I have an Android app that in the onCreate() method, preloads a lot of
I have Slider in my app that is connected to a few methods. In
I have a method like this in my RoR 3 app def buscar array

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.