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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:26:11+00:00 2026-05-22T02:26:11+00:00

I need some clarification/advice regarding modifying objects stored in mutable containers in Objective-C. Let’s

  • 0

I need some clarification/advice regarding modifying objects stored in mutable containers in Objective-C.
Let’s say I have an NSMutableArray called countries inside of which there are NSMutableDictionaries that store details about each country.

NSMutableArray *countries = [[NSMutableArray alloc] init];

NSMutabelDictionary *country = [[NSMutableDictionary alloc] init];
[country setObject: @"United States" forKey: @"Name"];

[countries addObject: country];

Later I want to modify a country stored in countries. I can do this either as:

[[countries objectAtIndex: 0] setObject: @"United States of America" forKey: @"Name"];

or as:

NSMutableDictionary *cnt = [countries objectAtIndex: 0];
[cnt setObject: @"United States of America" forKey: @"Name"];
[countries replaceObjectAtIndex: 0 withObject: cnt];

the result is the same. With my C++ background, I first thought that cnt would be a copy of the object that resides at position 0 in countries and after I modify it I have to put the modified object back into countries. However, array countries would still hold the updated country object even if I don’t send it the replaceObjectAtIndex message.

I would appreciate some advice on how to deal with such situations. Is it any better to explicitly replace modified objects in mutable containers with the new one, or is it completely unnecessary?

  • 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-22T02:26:12+00:00Added an answer on May 22, 2026 at 2:26 am

    In neither case is a copy of your NSMutableDictionary created. When you call [countries objectAtIndex: 0] you are getting a reference to the same NSMutableDictionary object that is in the countries array. Any modifications that you make to this object will therefore automatically be reflected in the instance that is returned by the countries array.

    In short, you have two pointers to a single object instance, and modifying the contents of a mutable collection type does not generate a copied instance of the collection.

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

Sidebar

Related Questions

I need some clarification. I have a Reportwriter dll that uses Crystal Reports. It
I need some clarification regarding runtime/heap issues when deleting an object which was created
I'm looking for some clarification regarding the behaviour of redirect_to . I have this
I need some clarification; I have a jar that I built and inside of
I just need some clarification on variables A normal variable has 2 parts to
Need some help about with Memcache. I have created a class and want to
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Just need some clarification on how to design a python script file test.py. When

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.