iOS - 声明具有固定列和未知行的2D数组(Objective-C)

时间:2017-06-29 18:40:22

标签: ios objective-c arrays

我想声明一个2d数组,其中我知道我需要的列数,但是想在程序运行时向每列添加行。我希望数组包含NSMutableDictionary对象。我该怎么做?

更新: 制作了一个2d数组,但在调用对象时崩溃了:

 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;
                        }
                    }

二维数组声明:

                    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"]);

1 个答案:

答案 0 :(得分:0)

我认为您正在currentEntry的{​​{1}}添加index 1个对象,然后在multiArray访问它。

我运行了你的代码并且运行正常:

index 0

另外,在将NSArray *multiArray = [NSArray arrayWithObjects: [NSMutableArray array], [NSMutableArray array], [NSMutableArray array], [NSMutableArray array], [NSMutableArray array],nil]; [[multiArray objectAtIndex:0] addObject:currentEntry]; NSLog(@"currentEntry Object: %@",[[multiArray objectAtIndex:0] objectAtIndex:0]); 添加到currentEntry之前,请确保mutableArray不为空。

希望它有所帮助!