NSJSON Loop Trouple

时间:2012-08-31 18:40:20

标签: iphone xcode ios5 nslog nsjsonserialization

我正在获取我的数据,但似乎每次发现我的“可保留”= 1时,数据都会在NSLog中发布;我认为这应该只发布一次,当我在tableview单元格中显示时,它会发布多次? 这是我的NSLog:

2012-08-31 11:35:39.682 GBSB[2168:15b03] These are the times avail: (
)
2012-08-31 11:35:39.683 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles"
)
2012-08-31 11:35:39.683 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles",
    "2012-08-31 08:15:00 America/Los_Angeles"
)
2012-08-31 11:35:39.683 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles",
    "2012-08-31 08:15:00 America/Los_Angeles",
    "2012-08-31 08:30:00 America/Los_Angeles"
)
2012-08-31 11:35:39.684 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles",
    "2012-08-31 08:15:00 America/Los_Angeles",
    "2012-08-31 08:30:00 America/Los_Angeles",
    "2012-08-31 08:45:00 America/Los_Angeles"
)
2012-08-31 11:35:39.684 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles",
    "2012-08-31 08:15:00 America/Los_Angeles",
    "2012-08-31 08:30:00 America/Los_Angeles",
    "2012-08-31 08:45:00 America/Los_Angeles",
    "2012-08-31 09:00:00 America/Los_Angeles"
)
2012-08-31 11:35:39.684 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles",
    "2012-08-31 08:15:00 America/Los_Angeles",
    "2012-08-31 08:30:00 America/Los_Angeles",
    "2012-08-31 08:45:00 America/Los_Angeles",
    "2012-08-31 09:00:00 America/Los_Angeles",
    "2012-08-31 09:15:00 America/Los_Angeles"
)

这是我正在使用的代码。

- (void)fetchedData:(NSData *)responseData {
    //parse out the json data

    NSError* error;

    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];


    NSDictionary* myslots =[json objectForKey:@"slots"];
    for (NSString *slotKey in myslots.allKeys) {
    NSDictionary *slot = [myslots valueForKey:slotKey];
        NSArray *tests = [myslots objectForKey:slotKey];
        NSMutableArray *timesArray = [[NSMutableArray alloc] init];
    for (NSDictionary *myDays in tests){

        if ([[myDays objectForKey:@"isReservable"] isEqualToNumber:[NSNumber numberWithInt:1]])
          [timesArray addObject:[myDays objectForKey:@"begin"]];

       NSLog(@"These are the times avail: %@", timesArray);

    }
    }

1 个答案:

答案 0 :(得分:0)

您似乎忘记了if声明的括号:

for (NSDictionary *myDays in tests){
    if ([[myDays objectForKey:@"isReservable"] isEqualToNumber:[NSNumber numberWithInt:1]]) {
        [timesArray addObject:[myDays objectForKey:@"begin"]];
        NSLog(@"These are the times avail: %@", timesArray);
    }
}

这样可以解决吗?测试一下,让我知道。