Iphone 多维数组问题
我正在基于API中的json数据为节/行创建多维数组。查看日志,添加行和节看起来不错,但当我记录这些节时,它只显示最后一个对象的一堆。似乎没有将行添加到节中 让最后一个API对象显示在x部分中,我做错了什么?x表示json计数<代码>自我预约是一个Iphone 多维数组问题,iphone,objective-c,json,ios5,Iphone,Objective C,Json,Ios5,我正在基于API中的json数据为节/行创建多维数组。查看日志,添加行和节看起来不错,但当我记录这些节时,它只显示最后一个对象的一堆。似乎没有将行添加到节中 让最后一个API对象显示在x部分中,我做错了什么?x表示json计数自我预约是一个NSArray* 根据我的看法,它应该是有效的 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray*
根据我的看法,它应该是有效的
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.
与:
您不断添加相同的行
对象,而不是新的,并不断更改内容,以便每次都显示最后的内容
尝试:
这样,每次都会添加一个新行
也许更好的是:
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
会释放它。但是,如果您使用方便的方法创建对象,那么它们将自动释放,无需采取进一步的操作来释放它们
还要注意,在比较字符串时,需要使用
IsequalString:
方法来比较内容,只需使用=
或=代码>仅比较字符串的地址。有效:)mutableCopy的确切用途是什么?另外,似乎在大约2个月的日期之后,它开始为每一行创建一个节,尽管节相同,但开始时间不同。我假设它设置了最后一个节,因为我使用了引用指针并取消了行数组的设置。正确吗?mutableCopy
可能是copy
,具体取决于进一步的使用。关键是创建一个新数组。向数组中添加对象只会添加点(并增加保留计数),多次添加同一对象只会添加同一指针,因此每次添加的内容都相同。最后一个内容就是所看到的内容。我编辑了您的代码帖子,其中包括[sections addObject:[rows mutableCopy]]代码>您前面提到过。在可能更好的代码中,不需要[rows mutableCopy]
,因为下一行创建了一个新数组,这是示例的重点,并且创建对象的方便方法,然后自动删除它们。因此,我回滚了编辑。
[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;