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

  • Home
  • SEARCH
  • 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 165865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:59:50+00:00 2026-05-11T11:59:50+00:00

I have this little code NSMutableArray *myArray = [[NSMutableArray alloc] init]; NSNumber *myNumber =

  • 0

I have this little code

 NSMutableArray *myArray = [[NSMutableArray alloc] init];   NSNumber *myNumber = [NSNumber numberWithDouble:752.65];   [myArray addObject:myNumber]; 

With this code I store Objects inside an array. But now I have two objects independent from each other.

If I change myNumber after it’s been added to the array the value inside the array does not change. How can I archive that? I tried to give a pointer only to the array but it did not work.

  • 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. 2026-05-11T11:59:51+00:00Added an answer on May 11, 2026 at 11:59 am

    You cannot put a variable into an array, and that’s what myNumber is: a variable. A variable is a container, and so is an array; the difference is that a variable is not also an object*, like the array is, and you can only put objects into an array.

    What you pass to addObject: is not the variable myNumber, but the object it contains. That’s what you are adding to the array.

    To add the variable instead of the object inside it, you would need to do addObject:&myNumber, in order to pass a pointer to the variable itself. But this won’t work, for two reasons:

    1. As I mentioned, the variable is not an object, and you can only add objects.
    2. Since this is a local variable, it will perish when the function exits; then you have a pointer to dead memory inside your array. When you go to access whatever’s at that pointer, your program would crash.

    There are three solutions that will work:

    1. As f3lix suggests, create a mutable number class, and create your number object from this class instead of NSNumber. You’ll need to override all the primitive methods of NSValue as described in the NSNumber documentation.
    2. Replace the object in the array instead of mutating it. Of course, this requires having access to the array from everywhere you’d want to change the number.
    3. Create a model object class that has the number as a property.

    That last solution is, in my opinion, the correct one. I doubt you are managing only a list of numbers; more likely, you are showing the user a list of something that has the number as a property. Model this in your code, and everything becomes much simpler.

    Your code after replacing the bare NSNumbers with model objects will be something like:

    MyModelObject *myModelObject = [[[MyModelObject alloc] init] autorelease]; [myModelObject setNumber:[NSNumber numberWithDouble:42.0]]; [myArray addObject:myModelObject];  //Some time later, you decide to change the number. [[myArray objectAtIndex:idx] setNumber:[NSNumber numberWithDouble:43.0]]; //Or: for (MyModelObject *obj in myArray) {     [obj setNumber:1000.0]; } 

    *I mean Cocoa objects. The C language does call any pointer, int, etc. an “object”, but this is a different definition.

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

Sidebar

Ask A Question

Stats

  • Questions 102k
  • Answers 102k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer maybe before, Tomcat was pretty unsecure, but nowadays... just anything… May 11, 2026 at 8:14 pm
  • Editorial Team
    Editorial Team added an answer I assume cr is an object. You're not creating a… May 11, 2026 at 8:14 pm
  • Editorial Team
    Editorial Team added an answer What you're running into is the boundary used to separate… May 11, 2026 at 8:14 pm

Related Questions

First, let me say that I'm a complete beginner at Python. I've never learned
I have this little function function makewindows(){ child1 = window.open (about:blank); child1.document.write(<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']),
I have this little function do connect to a MySQL database: function connectSugarCRM() {
I have a build script and as part of that script it copies a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.