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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:51:53+00:00 2026-05-14T07:51:53+00:00

I hope someone understands what happens to my NSMutableArray. I read records a, b,

  • 0

I hope someone understands what happens to my NSMutableArray.

I read records a, b, c, d from a database, load the fields into an object an add the object to an array. To do this I read the records into an instance of that object (tmpEvent) and add the Object to the target array (NSMutableArray myArray).

the code looks like:

for (condition) {
     tmpEvent.field1 = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 0)];
     tmpEvent.field2 = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 1)];
     tmpEvent.field3 = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 2)];

     NSLog(@"myArray: adding %@", tmpEvent.field1);
     [myArray addObject:tmpEvent];
}

The NSLog shows

 myArray: adding a
 myArray: adding b
 myArray: adding c
 myArray: adding d

Subsequent I enumerate the array (this can be in the same or a different method):

for (myObject *records in myArray) {
    NSLog(@"iEvents  value %@", records.field1);
}

The NSLog now shows:

 myArray value d
 myArray value d
 myArray value d
 myArray value d

a mystery …. ??? any thoughts?

  • 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-14T07:51:54+00:00Added an answer on May 14, 2026 at 7:51 am

    You need to allocate a new “iEvent” for each event. tmpEvent.field1 is pointing to the same place in memory for each subsequent add, therefore you are modifying the object that is already stored in the array. NSArray does not make a new copy of the object, just stores its pointer/address.

    One fix:

    [myArray addObject:[tmpEvent copy]];

    This assumes the members of the iEvent class conform to NSCopy.

    Another is to allocate a new tmpEvent for each event that you want to store.

    Question: But does this mean that when I free my array agein I need to enumerate through and “manually” release the objects again?

    Answer: You should send the object a release after adding it to the array as the array sends it a retain. When you dispose of the array, all objects are sent a release so you don’t have to. See below…

    Give the alloc option a try as conforming to NSCopying requires you to alloc an object anyway:

    for (condition) {
    tmpEvent = [[TmpEvent alloc] init]; // Or however it is initialized

     tmpEvent.field1 = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 0)];
     tmpEvent.field2 = ...
    
     [myArray addObject:tmpEvent];
    

    // NSArray retains objects that you add.
    [tmpEvent release];
    }

    —- To Conform to NSCopy —
    1. Class must inherit from NSCopying
    @interface TmpEvent : NSObject …

    1. Implement – (id) copyWithZone:(NSZone *) zone

      • (id)copyWithZone:(NSZone *)zone {
        TmpEvent *copyOfMyself = [[TmpEvent alloc] init];
        copyOfMyself.field1 = [self.field1 copy];
        …. etc.

      return copyOfMyself;
      }

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

Sidebar

Related Questions

I hope someone could help me on this. I want to add a month
I hope someone can guide me as I'm stuck... I need to write an
The docs at www.fastcgi.com aren't clear on this (to me), so I hope someone
This really, really urks me, so I hope that someone can give me a
Hope someone can advise. Having issues trying to remove a row once a link
Cant really find any useful information on this through Google so hope someone here
hope someone can help. I appreciate there are a few questions that are similar
hope someone can help me. I have a function like <script language=JavaScript type=text/javascript> function
Hi Hope someone can help me. My .net app that connects (via tcp) to
Hope anyone can shed light on this so I can use pens with dash

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.