e.ozmen
e.ozmen

Reputation: 253

NSMutableArray's objects are same

NSMutableDictionary *backprop;
NSMutableArray *backlist;

[backprop setValue:self.val forKey:@"id"];//NSLog for self.val
[backprop setValue:title.text forKey:@"name"];//NSLog for title.text
[backlist insertObject:backprop atIndex:backlist.count];

Here I add backprops to backlist. I add NSLogs different values. Thats what I want. But when I log them with this code:

while (i!=backlist.count) {
        NSLog(@"%@ %@",[[backlist objectAtIndex:i] objectForKey:@"id" ],[[backlist objectAtIndex:i] objectForKey:@"name" ]);i++;
    }

They all same with the last object. Why is this like that? Thank you.

Upvotes: 0

Views: 69

Answers (2)

e.ozmen
e.ozmen

Reputation: 253

I found it. I define NSMutableArray and NSMutableDictionary under @implementation in .m file.. I had to empty backprop.

So I changed my code:

-(void)....{
NSMutableDictionary *backprop = [[NSMutableDictionary alloc] init];
  [backprop setObject:self.val forKey:@"id"];
  [backprop setObject:title.text forKey:@"name"];

  [backlist addObject:backprop];NSInteger i=0;
  ...
}

Upvotes: 0

ahmedalkaff
ahmedalkaff

Reputation: 313

It is probably because you are using backlist.count-1 as your index instead of using i.

try this:

while (i!=backlist.count) {
        NSLog(@"%@ %@",[[backlist objectAtIndex:i] objectForKey:@"id" ],[[backlist objectAtIndex:i] objectForKey:@"name" ]);i++;
    }

Upvotes: 1

Related Questions