ndPPPhz ndPPPhz - 6 months ago 15
Objective-C Question

Objective-C : all of my array elements have the same value

I have a problem with my class.

I've written a

class that store my temporary situation of my array
but when I restore all of that, they all have the same value and exactly contains the copy of the last situation of my array
Going to the debugging area, I found that all of my array's elements have the same address but I can't understand why.

In my
I've the array
that has the actual condition and I want to save the past condition of this array in history

#import "Gioco.h"
#import "HistoryCard.h"

@interface Gioco()

@property (nonatomic,readwrite) int punteggioPartita;
@property (nonatomic,strong) NSMutableArray* carte; // of Card
@property (nonatomic,strong) NSMutableArray* history; // of HistoryCard Class


The array
contains instances of my class 'Card' .

@interface Card : NSObject

@property (nonatomic) BOOL chosen;
@property (nonatomic) BOOL match;


By UI, when I click on a
, I modify this the property of relative index in array 'carte'. It's works infact my
( game in english ), works perfectly . So I would create a History of my
and for this reason, I would "freeze" every changes of my card game and I've written this HistoryCard Class.
How you can see, every instances of this class, has this two properties that conserve the state of my game.

#import "Card.h"
@interface HistoryCard : Card

@property (nonatomic,readonly,strong) NSMutableArray* situationAtThisTime; //of Array
@property (nonatomic,readonly) int score;

andCurrentScore:(int) scoreSituation;
//designer initializzer


#import "HistoryCard.h"
#import "PlayingCard.h"

@interface HistoryCard()

@property (nonatomic,strong,readwrite) NSMutableArray* situationAtThisTime; //of Array
@property (nonatomic,readwrite) int score;


@implementation HistoryCard

andCurrentScore:(int) scoreSituation{
self=[super init];

if (self) {
self.situationAtThisTime=[NSMutableArray arrayWithArray:currentCardSituation];

return self;


When a touch arrives, starts a method in my
that changes the value of properties and for this reason I create a instances of
that would freeze the actual state of array 'carte' and add it in the array
of my card game

HistoryCard* currentHistory=[[HistoryCard alloc] initWithCurrentSituation:self.carte andCurrentScore:self.punteggioPartita];

[self.history addObject:currentHistory];

The problem is : if my
array contains 3 HistoryCard instances, every of that , has the score property set correctly ( the score of the game in the past ) but the
array, that are equals for all every instances and exactly contains the copy of the last situation of my array
It's like every array that I would save, points to the actual condition of my array

I've looking for here and I hadn't found anything that could help me.
Only a little help in this question but I've not global variables.

Thank you.

I found a personal solution but I don't know if there's a method that would make for me that.

NSMutableArray* now=[[NSMutableArray alloc]init];
for (Card*card in self.carte) {
Card* toSend=[[Card alloc]init];
[now addObject:toSend];

HistoryCard* currentHistory=[[HistoryCard alloc]initWithCurrentSituation:now
andCurrentScore: self.punteggioPartita];

[self.history addObject:currentHistory];

In this case I'm coping all of properties in a new instances of card.


self.situationAtThisTime=[NSMutableArray arrayWithArray:currentCardSituation]; copies the array but doesn't copy the cards in the array. Element 0 in each array points to the same card object. If you change this card then every element 0 is changed. You have to make a deep copy of the array with copies of the cards.

self.situationAtThisTime = [[NSMutableArray alloc] initWithArray:currentCardSituation copyItems:YES];