NSMutableArray和NSObject问题

时间:2011-05-28 10:01:45

标签: iphone loops nsmutablearray nsobject

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

NSDictionary * decodingJson = result;
   NSArray * users = [decodingJson 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);
}

}

我的NSobject是CheckinsA。我正在使用NSDictionary从响应字符串中收集数据。我在这里要做的是将每个CheckinsA保存在checkinArray中。这看起来很好:

`[checkinArray addObject:checkinsA];

test = [checkinArray objectAtIndex:count2];

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

并打印正确的值。但是当循环结束并且我尝试使用循环打印数组中的值时,只打印出最后一个CheckinsA !!我试着打印它:

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

(以防万一我的循环是问题,但它仍然只打印最后一个CheckinsA)。 那么为什么我的整个数组只用最后一个CheckinsA填充?这很奇怪!

3 个答案:

答案 0 :(得分:1)

每次都要向数组添加相同的对象。像这样移动代码:

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

答案 1 :(得分:0)

每次循环时都需要初始化一个新对象。否则你正在改变同一个对象。

Checkin *checkinsA = [[Checkin alloc] init];作为循环中的第一行,在完成向数组添加checkinsA后的最后一行,执行[checkinsA release];

答案 2 :(得分:0)

通过
检查你的阵列 NSLog(@&#34;%@&#34;,[checkinArray description]);