0
votes
Checkin *checkinsA = [[Checkin alloc] init]; 

NSDictionary *decodedJson = result;
NSArray *users = [decodedJson objectForKey:@"data"]; Checkin *test = [[Checkin alloc] init];

for(NSDictionary *user in users) {
    NSLog(@"Created item: %@ \n", [user objectForKey:@"created_time"]);
     checkinsA.time = [NSString stringWithFormat:@"%@",[user objectForKey:@"created_time"]];
    NSDictionary *fromData = [user objectForKey:@"from"];
     NSLog(@"user id is: %@ \n", [fromData objectForKey:@"id"]);
      checkinsA.profID = [fromData objectForKey:@"id"];

     NSLog(@"user name is: %@\n ", [fromData objectForKey:@"name"]);
      checkinsA.name =[fromData objectForKey:@"name"];

    NSDictionary *placeData = [user objectForKey:@"place"];
    NSDictionary *locationData = [placeData objectForKey:@"location"];
     NSLog(@"City: %@ \n", [locationData objectForKey:@"city"]);
      checkinsA.city = [locationData objectForKey:@"city"];

     NSLog(@"Country: %@ \n", [locationData objectForKey:@"country"]);
      checkinsA.country = [locationData objectForKey:@"country"];

     NSLog(@"Latitude: %@ \n", [locationData objectForKey:@"latitude"]);
     checkinsA.lat = [locationData objectForKey:@"latitude"];

     NSLog(@"Longitude: %@ \n", [locationData objectForKey:@"longitude"]);
     checkinsA.lon = [locationData objectForKey:@"longitude"];

     NSLog(@"Place name: %@ \n", [placeData objectForKey:@"name"]);
     checkinsA.place = [placeData objectForKey:@"name"];

    NSDictionary *tagData = [user objectForKey:@"tags"];
    NSArray *tagDataArray = [tagData objectForKey:@"data"];
    for(NSDictionary *tagData2 in tagDataArray){
      NSLog(@"tagged user id is: %@ \n", [tagData2 objectForKey:@"id"]);
        [checkinsA.taggedID addObject:[tagData2 objectForKey:@"id"]];
      NSLog(@"tagged user name is: %@\n ", [tagData2 objectForKey:@"name"]);
         [checkinsA.taggedName addObject:[tagData2 objectForKey:@"name"]];
    }

    [checkinArray addObject:checkinsA];
    test = [checkinArray objectAtIndex:count2];
    NSLog(@"Check array: %@",test.name);
    count2++;
}
for(int i=0;i<count2;i++)
{
    test = [checkinArray objectAtIndex:i];
    NSLog(@"%@",test.name);
}

}

My NSobject is the CheckinsA. I am collecting data from a response string using NSDictionary. What I am trying to do here is to save each CheckinsA in a the checkinArray.This looks fine:

`[checkinArray addObject:checkinsA];

test = [checkinArray objectAtIndex:count2];

NSLog(@"Check array: %@",test.name);`

and is printing the right value. But when the loop is finished and I am trying to print the values from the array using a loop is printing out only the last CheckinsA!! I tried to print it with:

test = [checkinArray objectAtIndex:0];
NSLog(@"%@",test.name);
test = [checkinArray objectAtIndex:1];
NSLog(@"%@",test.name);

(just in case that my loop was the problem but its still printing only the last CheckinsA). So why my whole array is filled only with the last CheckinsA? It's weird!

3

3 Answers

1
votes

You're adding the same object to the array every time. Move code around like this:

Checkin *checkinsA
for(NSDictionary *user in users) {
    checkinsA = [[Checkin alloc] init];
    // loop code
    [checkinArray addObject:checkinsA];
    [checkinsA release];
}
0
votes

You need to initialize a new object every time you loop. Otherwise you are changing the same object.

make Checkin *checkinsA = [[Checkin alloc] init]; the first line in the loop and at the end after you are done adding checkinsA to the array, do [checkinsA release];

0
votes

check your array by
NSLog(@"%@",[checkinArray description]);