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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:48:58+00:00 2026-05-23T06:48:58+00:00

//viewController.h file //——————— #import <UIKit/UIKit.h> @interface ItemClass : NSObject { NSString* name; } @property

  • 0
//viewController.h file
//---------------------
#import <UIKit/UIKit.h>

@interface ItemClass : NSObject 
{
    NSString* name;
}
@property (nonatomic, retain) NSString* name;
@end



@interface PlaceClass : ItemClass 
{
    NSString* coordinates;
}
@property (nonatomic, retain) NSString* coordinates;
@end




@interface viewController : UIViewController {
    NSMutableArray* placesMutArray;
    PlaceClass* currentPlace;
}

@end

//viewController.m file
//------------------------

#import "viewController.h"
@implementation ItemClass
@synthesize name;
@end


@implementation PlaceClass
@synthesize coordinates;
@end


@implementation viewController

- (void)viewDidLoad {
    [super viewDidLoad];

    placesMutArray = [[NSMutableArray alloc] init];
    currentPlace = [[PlaceClass alloc] init];

    // at some point in code the properties of currentPlace are set
    currentPlace.name = [NSString stringWithFormat:@"abc"];
    currentPlace.coordinates = [NSString stringWithFormat:@"45.25,24.22"];
    // currentPlace added to mutable array
    [placesMutArray addObject:currentPlace];

    //now the properties of currentPlace are changed
    currentPlace.name = [NSString stringWithFormat:@"def"];
    currentPlace.coordinates = [NSString stringWithFormat:@"45.48,75.25"];
    // again currentPlace added to mutable array
    [placesMutArray addObject:currentPlace];

    for(PlaceClass* x in placesMutArray)
    {
        NSLog(@"Name is : %@", x.name);
    }

}
@end

output i get :

Name is : def
Name is : def

desired output :

Name is : abc
Name is : def

I want the placesMutArray to have two separate objects (each allocated separate memory space) each with their own set of “name” and “coordinates” property. But the above code, apparently, just changes the property of same object ‘currentPlaces’ and its reference gets added to the array twice. Implying i only get one object allocated in memory. When i traverse the array using fast enumeration and NSlog the name property for both elements i jut get last set value twice.

Can adopting NSCopying protocol solve the problem?

[placesMutArray addObject:[currentPlace copy]];

If yes, then how should i go about it? I tried but i am getting lot of errors.

  • 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-23T06:48:59+00:00Added an answer on May 23, 2026 at 6:48 am

    you’re right in that you’re using the same instance. You just need to make a new one.

    Try this :

    // Create a second PlaceClass before setting it's properties to 'def'
    currentPlace = [[PlaceClass alloc] init];
    currentPlace.name = [NSString stringWithFormat:@"def"];
    currentPlace.coordinates = [NSString stringWithFormat:@"45.48,75.25"];
    

    Adding an object to an array doesn’t copy the object for you – it just means that the array knows about the object. After you added the first currentPlace to the array your currentPlace variable was still pointing to the first object so when started to set new name and coordinates you were updating the first one, not creating a new one.

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

Sidebar

Related Questions

I have a ViewController, i declare NSString *xTitle at the top in testViewController.m file
I have a View in a xib file and a corresponding ViewController. With Interface
I have a global NSString variable that I declared in my ViewController.m file, outside
So say in my viewcontroller's .h file I have: @interface MyViewController : UIViewController {
I'm asking my ViewController for it's view.center property and drawing a new UIView that
I want to declare nsstring in uiviewcontroller.h file and use this string as whole
I have a .xib file for my viewController. It contains a TableView and a
From my viewcontroller view I am loading a xib file. I want this view
In my current project inside the file ViewController.m, I am running the method: [[connection
I have a viewController showing some content. In header file, I defined an instance

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.