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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:28:27+00:00 2026-06-02T20:28:27+00:00

I have another question for you that I believe I already know WHY it

  • 0

I have another question for you that I believe I already know WHY it isn’t working, but I’m not sure how to fix it…

I have a database array object using MutableNSArray. Let’s call it “Database”. This is an array of objects of type DatabaseRecord. I also have another DatabaseRecord object called CurrentRecord which is the active object I manipulate from the user interface. When I add a new entry into the database, I call:

[Database addObject: CurrentRecord];

When I’m done making changes, I replace the added record in the database with:

[Database replaceObjectAtIndex: <index> withObject: CurrentRecord];    

What I just realizes was that CurrentRecord is a pointer defined as:

DatabaseRecord *CurrentRecord;

I believe that what is happening is that each and every new entry in the database sets itself to the values of CurrentRecord. Likewise, when I change CurrentRecord, all of objects in the NSArray modify themselves accordingly, so I’m left with a database of, say, 100 records that are all identical.

What I want to do is essentially COPY the values of CurrentRecord into the appropriate object in the MutableNSArray.

I’m sure there’s an easy fix to this, such as not passing a pointer, but I’m not quite sure how to proceed. I hope I’m being clear in my question…

  • 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-02T20:28:29+00:00Added an answer on June 2, 2026 at 8:28 pm

    When I’m done making changes, I replace the added record in the database

    If you’re changing records that are already in the array, there’s no need to replace anything. Do this:

    DatabaseRecord *currentRecord;
    
    currentRecord = [database objectAtIndex:someIndex];
    currentRecord.name = @"tom";
    currentRecord.location = @"los angeles";
    
    currentRecord = [database objectAtIndex:someOtherIndex];
    currentRecord.name = @"harry";
    currentRecord.location = @"new york";
    

    Now you’ve just changed the contents of two of the records in the database — no need to replace anything. The reason you can do this is that currentRecord is just a pointer to an object that already exists.

    If you want to modify every record in the array, you can do:

    for (currentRecord in database) {
        // make changes to currentRecord here
        // no need to replace the record in the array -- it's already there!
    }
    

    When you’re adding new records to the database, on the other hand, you’ll need to make sure that you create new objects each time. You can do that by copying an existing object, or by instantiating a new object each time yourself:

    for (int i = 0; i < 10; i++) {
        currentRecord = [[DatabaseRecord alloc] init];
        [database addObject:currentRecord];
        // add [currentRecord release]; for non-ARC builds
    }
    

    or:

    currentRecord = [[DatabaseRecord alloc] init];
    for (int i = 0; i < 10; i++) {
        [database addObject:[currentRecord copy]];
        // make the previous line [database addObject: [[currentRecord copy] autorelease]]; for non-ARC builds
    }
    

    If you’re not using ARC, you’ll need to release the objects you create appropriately, as shown in the comments above.

    I’m sure there’s an easy fix to this, such as not passing a pointer

    Nope. The only way to refer to an object in Objective-C is via pointer. The fix isn’t difficult, though — you just need to understand how pointers work a little better.

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

Sidebar

Related Questions

Another question related to this one . I have a List<SortableObjects> that is the
Another lame question So, I have a site that displays several students' requests to
Another WPF question for you all. I have a user control that contains a
I have a question regarding the creation of a utility that executes another command.
--EDIT-- I believe this is a valid question that may have multiple answers (as
I have a question very similar to this one but the answer does not
I need help with a question that I could not answer yet. I have
Let's say you have a homepage that it's different (but not totally) from the
I have an other active question HERE regarding some hopeless memory issues that possibly
i have another question on getting my XML serialization neat, which i can't seem

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.