多维数组问题

时间:2012-01-25 17:51:07

标签: iphone objective-c json ios5

我正在基于来自API的json数据为节/行创建一个多维数组。查看日志,添加的行和部分看起来不错,但是当我记录这些部分时,它只显示了一堆最后一个对象。似乎没有将行添加到部分中。

在x次的部分中显示最后一个API对象我做错了什么? x代表json计数。 self.appointmentsNSArray *

根据objective-c multi-dimensional array它应该有用。

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        NSMutableArray *sections = [[NSMutableArray alloc] init];
        NSMutableArray *rows = [[NSMutableArray alloc] init];
        NSString *lastDate = nil;
        for (NSDictionary *dict in [json objectForKey:@"data"]) {
            NSString *date = [dict objectForKey:@"date"];
            NSLog(@"Dates: %@ - %@", date, lastDate);
            if (date != lastDate) {
                if (lastDate == nil) {
                    NSLog(@"Adding Row 1");
                    [rows addObject:dict];
                    lastDate = date;
                } else {
                    NSLog(@"Adding Section 1");
                    NSLog(@"Adding #rows %i",[rows count]);
                    [sections addObject:rows];
                    [rows removeAllObjects];
                    NSLog(@"Adding Row 2 %@",[dict objectForKey:@"start_time"]);
                    [rows addObject:dict];
                    lastDate = date;
                }
            } else {
                NSLog(@"Adding Row 3");
                [rows addObject:dict];
            }
        }
        if (rows) {
            NSLog(@"Adding Section 2");
            NSLog(@"Adding #rows %i",[rows count]);
            [sections addObject:rows];
        }
        NSLog(@"Sections: %@", sections);

        self.appointments = [sections mutableCopy]; //I have also tried self.appointments = sections

        sections = nil;
        rows = nil;

日志显示

Sections: (
        (
                {
            abbrev = "";
            account = "";
            "addOnService_id" = "";
            alert = "";
            "appt_id" = 1839384;
            "appt_id_unique" = 1839384;
            "appt_status_description" = "";
            "appt_status_type" = "";
            "c_id" = 47;
            cost = "0.00";
            "coupon_id" = "";
            "creation_emp_id" = 2288;
            "creation_timestamp" = 201111040717;
            "customer_id" = 0;
            "customer_notes" = "";
            "customer_package_id" = "";
            date = 20121228;
            "employee_id" = 2288;
            "employee_notes" = "";
            employer = "";
            "end_time" = 570;
            "first_name" = "";
            "history_id" = 1830959;
            key = 134;
            "last_emp_id" = 2288;
            "last_name" = "";
            "last_timestamp" = 201111040717;
            "lead_description" = "";
            "link_id" = 0;
            "location_name" = "Telephonic Setup/Training";
            "make_id" = "";
            "middle_name" = "";
            "model_id" = "";
            "model_year" = "";
            name = "My Name ";
            odometer = "";
            "other_vehicle" = "";
            "package_name" = "";
            "payment_type_description" = "";
            "payment_type_id" = "";
            "pet_id" = "";
            "po_number" = "";
            reason = "B.O.B";
            "rebook_id" = "";
            "recur_id" = 20954;
            "rep_id" = "";
            "room_id" = 0;
            "room_name" = "";
            service = "";
            "service_id" = 0;
            "service_time_description" = "";
            spots = 1;
            "staff_screen_name" = "John Smith";
            "staff_type_id" = 0;
            "start_time" = 540;
            "status_id" = 0;
            tip = "";
            "type_id" = 8;
            vin = "";
        }
    ),
        (
                {
            abbrev = "";
            account = "";
            "addOnService_id" = "";
            alert = "";
            "appt_id" = 1839384;
            "appt_id_unique" = 1839384;
            "appt_status_description" = "";
            "appt_status_type" = "";
            "c_id" = 47;
            cost = "0.00";
            "coupon_id" = "";
            "creation_emp_id" = 2288;
            "creation_timestamp" = 201111040717;
            "customer_id" = 0;
            "customer_notes" = "";
            "customer_package_id" = "";
            date = 20121228;
            "employee_id" = 2288;
            "employee_notes" = "";
            employer = "";
            "end_time" = 570;
            "first_name" = "";
            "history_id" = 1830959;
            key = 134;
            "last_emp_id" = 2288;
            "last_name" = "";
            "last_timestamp" = 201111040717;
            "lead_description" = "";
            "link_id" = 0;
            "location_name" = "Telephonic Setup/Training";
            "make_id" = "";
            "middle_name" = "";
            "model_id" = "";
            "model_year" = "";
            name = "My Name ";
            odometer = "";
            "other_vehicle" = "";
            "package_name" = "";
            "payment_type_description" = "";
            "payment_type_id" = "";
            "pet_id" = "";
            "po_number" = "";
            reason = "B.O.B";
            "rebook_id" = "";
            "recur_id" = 20954;
            "rep_id" = "";
            "room_id" = 0;
            "room_name" = "";
            service = "";
            "service_id" = 0;
            "service_time_description" = "";
            spots = 1;
            "staff_screen_name" = "John Smith";
            "staff_type_id" = 0;
            "start_time" = 540;
            "status_id" = 0;
            tip = "";
            "type_id" = 8;
            vin = "";
        }
    ), ... over and over again.

1 个答案:

答案 0 :(得分:2)

使用:

[sections addObject:rows];
[rows removeAllObjects];
[rows addObject:dict];

您继续添加相同的rows对象而不是新对象,并不断更改内容,以便每次显示最后的内容。

尝试:

[sections addObject:[rows mutableCopy]]; 
[rows removeAllObjects];
[rows addObject:dict];

这样每次都会添加一个新行。

也许更好:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *sections = [NSMutableArray array];
NSMutableArray *rows = [NSMutableArray array];
NSString *lastDate = nil;
for (NSDictionary *dict in [json objectForKey:@"data"]) {
    NSString *date = [dict objectForKey:@"date"];
    if ([date isEqualToString:lastDate] == NO) {
        if (lastDate == nil) {
            [rows addObject:dict];
            lastDate = date;
        } else {
            [sections addObject:rows];
            rows = [NSMutableArray array];
            [rows addObject:dict];
            lastDate = date;
        }
        [rows addObject:dict];
    }
}
if (rows) {
    [sections addObject:rows];
}

self.appointments = sections;

请注意,为对象分配0不会释放它,release会释放它。但是如果你使用便捷方法创建对象,那么它们是自动释放的,不需要进一步的操作来释放它们。

另请注意,在比较字符串时,需要使用isEqualToString:方法来比较内容,只需使用=!=仅比较字符串的地址。