views:

40

answers:

1

I want to add element in the array dynamically. For this I am using

[myArray addObject:myword];

myword is a NSMutableString type object.

on each button click myword get changes. But all the element of array store the last value. Suppose first time the array has 1 element = "me" Second time array should have 2 element = "me", "you". But it shows "me", "me". what may be the problem?

+3  A: 

Since you're probably storing the same instance of NSMutableString over and over again, it's natural that changing it changes "all" elements. After all, they all point to the same object.

Try:

[myArray addObject:[[myword copy] autorelease]];

Or if you need to have NSMutableStrings:

[myArray addObject:[[myword mutableCopy] autorelease]];

You need the autorelease here, otherwise you'd have a memory leak.

DarkDust