我要聲明一個二維陣列,其中我知道我需要的列數,但要在程序運行時將行添加到每一列的二維陣列。我想讓數組保存NSMutableDictionary對象。我將如何做到這一點?的iOS - 聲明與固定柱和未知的行(目標C)
UPDATE: 由二維數組但調用對象時崩潰:
for (int i = 0; i < [resultSet count]-1; ++i){
NSMutableDictionary *temp = (NSMutableDictionary *) [resultSet objectAtIndex:i];
if (temp == nil) break; // Break if nil
NSMutableDictionary * currentEntry = [[NSMutableDictionary alloc] init];
//store the firstname, lastname, userid, starting and ending dates
[currentEntry setObject:[temp objectForKey:@"ticket_id"] forKey:@"ticket_id"];
[currentEntry setObject:[temp objectForKey:@"title"] forKey:@"title"];
[currentEntry setObject:[temp objectForKey:@"user_id"] forKey:@"user_id"];
[currentEntry setObject:[temp objectForKey:@"date_submitted"] forKey:@"date_submitted"];
[currentEntry setObject:[temp objectForKey:@"date_resolved"] forKey:@"date_resolved"];
[currentEntry setObject:[temp objectForKey:@"handled_by"] forKey:@"handled_by"];
[currentEntry setObject:[temp objectForKey:@"status"] forKey:@"status"];
[currentEntry setObject:[temp objectForKey:@"category"] forKey:@"category"];
[currentEntry setObject:[temp objectForKey:@"priority"] forKey:@"priority"];
[currentEntry setObject:[temp objectForKey:@"description"] forKey:@"description"];
[currentEntry setObject:[temp objectForKey:@"resolution"] forKey:@"resolution"];
//stored in db in enum as "school" this translates it to "campus"
//maintains backwards compatability with db and previous app versions
[currentEntry setObject:[temp objectForKey:@"location"] forKey:@"location"];
[currentEntry setObject:[temp objectForKey:@"date_modified"] forKey:@"date_modified"];
[currentEntry setObject:[temp objectForKey:@"room_no"] forKey:@"room_no"];
[currentEntry setObject:[temp objectForKey:@"submitter_name"] forKey:@"submitter_name"];
[currentEntry setObject:[temp objectForKey:@"submitter_surname"] forKey:@"submitter_surname"];
[currentEntry setObject:[temp objectForKey:@"item_type"] forKey:@"item_type"];
NSLog(@"ITEMS: %@", [currentEntry objectForKey:@"item_type"]);
int k = 0;
for (int i = 0; i < 28; i++) {
if ([[currentEntry objectForKey:@"item_type"] isEqualToString:items[i]]) {
myArray[i] += 1;
k = i;
}
}
2D陣列的聲明:
multiArray = [NSArray arrayWithObjects:
[NSMutableArray array],
[NSMutableArray array],
[NSMutableArray array],
[NSMutableArray array],
[NSMutableArray array],
[NSMutableArray array],
[NSMutableArray array],
[NSMutableArray array],nil];
更多的代碼:
if ([[currentEntry objectForKey:@"handled_by"] isEqualToString:@"unassigned"]) {
[currentEntry setObject:@"unassigned" forKey:@"handler_name"];
[currentEntry setObject:@"" forKey:@"handler_surname"];
}
else {
[currentEntry setObject:[temp objectForKey:@"handler_name"] forKey:@"handler_name"];
[currentEntry setObject:[temp objectForKey:@"handler_surname"] forKey:@"handler_surname"];
//}
}
將對象添加到2D和1d陣列
if([tabbarData.ticketListViewMode isEqualToString:@"Maintenance"] ||
[tabbarData.ticketListViewMode isEqualToString:@"Cleaning"] ||
[tabbarData.ticketListViewMode isEqualToString:@"I.T."] ||
[tabbarData.ticketListViewMode isEqualToString:@"viewAll"]){
LoginData *login = [self dataObject];
if([login.userRole isEqualToString:@"MR"] || [login.userRole isEqualToString:@"AS"] ||
([login.category isEqualToString:[temp objectForKey:@"category"]] &&
([login.userRole isEqualToString:@"CM"] || [login.userRole isEqualToString:@"UR"]))){
[ticketList addObject:currentEntry];
[ticketInformation addObject: currentEntry];
[[multiArray objectAtIndex:0] addObject:currentEntry];
}
}
else{
[ticketList addObject:currentEntry];
[ticketInformation addObject: currentEntry];
[[multiArray objectAtIndex:0] addObject: currentEntry];
}
}
這種崩潰
NSLog(@"word: %@", [[[multiArray objectAtIndex:0] objectAtIndex:5] objectForKey:@"priority"]);
這不會崩潰
NSLog(@"word1: %@", [[ticketList objectAtIndex:5] objectForKey:@"priority"]);
一個數組持有字典的數組。聽起來很OOP。向我們展示迄今爲止已嘗試過的內容 – crizzis
已更新的問題 – apex
請在添加到數組之前檢查currentEntry對象(如果爲null)。 – shuvo