私は、APIのjsonデータに基づいてセクション/行の多次元配列を作成しています。ログを見ると、追加する行とセクションは見た目がよくなりますが、セクションをnslogすると、最後のオブジェクトだけが表示されます。行がセクションに追加されていないようです。多次元配列の問題
最後のAPIオブジェクトをセクションx回に表示するにはどうしたらよいですか? xはjson数を表す。 self.appointments
はNSArray *
です。objective-c multi-dimensional arrayによれば、それはうまくいくはずです。あなたは同じrows
オブジェクトではありません新しいものを追加し、最後の内容が毎回表示されるように内容を変え続ける保つ
[sections addObject:rows];
[rows removeAllObjects];
[rows addObject:dict];
:
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.
私は正確にmutableCopyは何ですか?また、セクションが同じだがstart_timesが異なっていても、約2ヶ月の日付の後に、各行のセクションの作成を開始するように見えます。 – Bot
私は参照ポインタを使用していて、行配列を設定していないので、最後のものを設定していたと仮定します。正しい? – Bot
'mutableCopy'はそれ以降の使用法に応じて' copy'になります。ポイントは、新しい配列を作成することです。オブジェクトを配列に追加するだけでポイントが追加され(そして保持カウントがアップ)、同じオブジェクトを複数回追加すると、同じポインタが追加され、毎回同じ内容が追加されます。最後の内容は見られるものです。 – zaph