我有一个充满字典的数组,每天一个。在每个字典里面我都有一个练习列表,每个练习里面都有一个带有两个值的数组。我写的程序有几个训练程序的选择器,保存为一个名为programInt的整数。我还有一个方法将这个Int转换为一个名为programPickerText的字符串。这是我到目前为止的代码:
// create array
self.liftData = [NSMutableArray array];
// fill array with dictionaries, one for each day
for (int i = 0; i < 5; i++){
[self.liftData setObject:[NSMutableDictionary dictionary] atIndexedSubscript:i];
// create an array for exercises
self.liftData[i][@"Exercise1"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
self.liftData[i][@"Exercise2"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
self.liftData[i][@"Exercise3"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
self.liftData[i][@"Exercise4"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
self.liftData[i][@"Exercise5"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
self.liftData[i][@"Exercise6"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
NSLog(@"array is %@", self.liftData);
}
我基本上需要我的可变数组(self.liftData)才能以下列格式保存信息:
self.liftData[program][day][@"Exercise1"][0]
目前它保存的数据如下:
self.liftData[Day][@"Exercise1"][0]
答案 0 :(得分:2)
有时候实际使用命名变量非常有用
// Create an array of programs
NSMutableArray* programArray = [NSMutableArray array];
self.liftData = programArray;
for (int programIndex = 0; programIndex < ....???...) {
// Add an array of days to the program
NSMutableArray* dayArray = [NSMutableArray array];
[programArray addObject:dayArray];
// fill dayArray with dictionaries, one for each day
for (int dayIndex = 0; dayIndex < 5; dayIndex++){
// Add a dictionary of exercises to the day
NSMutableDictionary* exerciseDict = [NSMutableDictionary dictionary];
[dayArray addObject:exerciseDict];
// Fill exerciseDict with exercises
exerciseDict[@"Exercise1"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
exerciseDict[@"Exercise2"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
exerciseDict[@"Exercise3"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
exerciseDict[@"Exercise4"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
exerciseDict[@"Exercise5"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
exerciseDict[@"Exercise6"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
}
}
NSLog(@"The program array is %@", self.liftData);
答案 1 :(得分:1)
你能再添加一个NSMutableArray吗?
// create array
self.liftData = [NSMutableArray array];
NSMutableArray *programmArray = [NSMutableArray array];
[self.liftData addObject:programmArray];
// fill array with dictionaries, one for each day
for (int i = 0; i < 5; i++){
[programmArray setObject:[NSMutableDictionary dictionary] atIndexedSubscript:i];
// create an array for exercises
programmArray[i][@"Exercise1"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
programmArray[i][@"Exercise2"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
programmArray[i][@"Exercise3"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
programmArray[i][@"Exercise4"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
programmArray[i][@"Exercise5"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
programmArray[i][@"Exercise6"] = [NSMutableArray arrayWithObjects:@"",@"", nil];
NSLog(@"array is %@", self.liftData);
}
答案 2 :(得分:1)
我认为Avt的答案错过了一层。在回答之前,让我这样说:有时当您的数据表示变得复杂且足够深时,创建一些类并抽象出内部表示是有意义的,这样您就可以使用可读的方法名称。例如,[myProgram getExerciseArrayForDay:CONST_WEEKDAY_TUESDAY];
这不仅使代码更易读,更容易实现,而且如果更改基础数据表示,代码更改也是非常本地化的。也就是说,这是一种使用大量方法来避免让自己混淆的方法。
self liftData = [NSMutableDictionary dictionary];
[self.liftData addObject:[self newProgram] forKey:@"firstProgram"];
-(NSMutableArray *) newProgram(){
NSMutableArray *program = [NSMutableArray array];
for(int i = 0; i < 7; i++) {
[program addObject:[self newDayExercises]];
}
return program;
}
-(NSMutableDictionary *) newDayExercises {
static NSArray *keyNames = @[@"Exercise1", @"Exercise2", @"Exercise3", @"Exercise4", @"Exercise5", @"Exercise6"];
NSMutableDictionary *dayExercises = [NSMutableDictionary dictionary];
for(NSString *keyName in keyNames) {
[dayExercises addObject:@{@"", @""} forKey:keyName]; // note handy NSDictionary initializer
}
return dayExercises;
}