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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:27:51+00:00 2026-05-27T16:27:51+00:00

With the following code, both items in the array are the same (the last

  • 0

With the following code, both items in the array are the same (the last item). What am I doing wrong that is causing this array to overwrite the values? I’m trying to use 1 object so I don’t have to instantiate X number of objects.

self.myArray = [[NSMutableArray alloc] init];

MyObjClass *obj = [[MyObjClass alloc] init];
obj.firstName = @"First Name";
obj.lastName = @"Last Name";
obj.created = @"Dec 17 16:24";
[self.myArray addObject:obj];

obj.firstName = @"First Name2";
obj.lastName = @"Last Name2";
obj.created = @"Dec 18 7:41";
[self.myArray addObject:obj];

In MyObjClass.h I have @interface MyObjClass : NSObject. Is NSObject the wrong datatype?

Properties in MyObjClass.h:

@property (strong) NSString *firstName;
@property (strong) NSString *lastName;

And from MyObjClass.m:

@synthesize firstName, lastName;
  • 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-27T16:27:52+00:00Added an answer on May 27, 2026 at 4:27 pm

    The array isn’t overwriting the values, you are in your code. You have one instance of the MyObjClass. *obj is a pointer to that objects and when you add it to the array twice, the array has two indexes that point to the object you added twice.

    By setting the properties on obj, you’re changing the values of that one object that both your *obj pointer and the array points to.

    Why are you concerned about instantiating X objects? It sound like you want X objects which are in the array with distinct values.

    Besides creating X objects, you can copy the first object, set the values then add that to the array but since you’re setting all the values anyways, I’m not sure why you just wouldn’t init a new object.

    EDIT:

    Based on your comment below, it looks like your concern of multiple objects is around memory management. When you add an object to an array, it retains the object so after you add it (if you’re done with it in that scope), then release or autorelease it. When the array is released, it will call release on all the objects in the array. You need n objects whether you init or copy – you still have multiple. Release them and then let the array release when it’s released.

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

Sidebar

Related Questions

In the following code, both amp_swap() and star_swap() seems to be doing the same
I have the following code $result = $handle->select()->from('store_products_id', array('count'=>'COUNT(store_products_id.product_id)')) ->where('store_products_id.store_id=?', $this->store_id) ->columns($selectColumns) ->join('product_quickinfo', 'store_products_id.product_id
In the following code, both the INPUT and TEXTAREA elements render wider than they
I try to use the following code: ArrayList<String> Map<String, String> Eclipse complains about both
I have the following code: ViewData.Model = from m in dataModel.Items where m.Loaned ==
I am trying to create a ListView using the following code: setListAdapter(new ArrayAdapter<String>(this, R.layout.settings_items,
I have a JSON feed that I'm parsing. Here's the following code samples: JSON
FOUND THAT CStarRating is causing this error if it is removed it gets ok.Also
I have the following code in an external JavaScript file that is called within
I'm quite new to both ASP.Net and MVC. I got the following code in

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.