我这样做是在应用程序启动后立即执行的,幸运的是我只需要在一个名为 CMIDataManager 的单例类中执行一次,我的应用程序启动时间太长。
该 plist 包含:
指挥官.plist:
德语 - 数组
苏联 - 阵列
每个指挥官阵列有 19 个指挥官,每个指挥官有 5 种能力(通过独特的能力 uid 映射)。
能力.plist:
GermanAbilities - 数组
苏联能力 - 阵列
每个数组包含 40 个具有 uid 的能力(用于将指挥官映射到能力)
一开始,我需要创建一个模型类,因此我针对每个隐藏的能力迭代指挥官的能力 uid,一旦找到匹配项,我将能力模型对象添加到 Commaders 模型对象。
我怎样才能更快地做到这一点?使用基于块的枚举会加快速度吗?我该如何使用它?
-(void)loadCommandersAndAbilities{
#pragma German Abilities iteration
NSString* abilitiesPlistPath = [[NSBundle mainBundle] pathForResource:@"Abilities" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:abilitiesPlistPath];
NSArray *tempArray = [dictionary objectForKey:@"GermanAbilities"];
NSArray *tempArray2 = [dictionary objectForKey:@"SovietAbilities"];
NSMutableArray *tempAbilitiesArray = [[NSMutableArray alloc] initWithCapacity:tempArray.count];
for (NSDictionary *dict in tempArray) {
Ability *ability = [[Ability alloc] init];
[ability populateWithDictionary:dict];
[tempAbilitiesArray addObject:ability];
NSLog(@"Adding object %@ to temp abilities",ability.name);
}
self.germanAbilitiesArray = [NSArray arrayWithArray:tempAbilitiesArray];
[tempAbilitiesArray removeAllObjects];
#pragma Soviet abilities iteration
for (NSDictionary *dict in tempArray2) {
Ability *ability = [[Ability alloc] init];
[ability populateWithDictionary:dict];
[tempAbilitiesArray addObject:ability];
}
self.sovietAbilitiesArray = [NSArray arrayWithArray:tempAbilitiesArray];
#pragma German commander itertation
NSString* commandersPlistPath = [[NSBundle mainBundle] pathForResource:@"Commanders" ofType:@"plist"];
dictionary = [[NSDictionary alloc] initWithContentsOfFile:commandersPlistPath];
tempArray = [dictionary objectForKey:@"German"];
tempArray2 = [dictionary objectForKey:@"Soviet"];
NSLog(@"Temp German commadner array is %@", tempArray);
NSLog(@"Temp Soviet commadner array is %@", tempArray2);
NSMutableArray *tempCommandersArray = [[NSMutableArray alloc] initWithCapacity:tempArray.count];
NSMutableArray *tempCommandersArray2 = [[NSMutableArray alloc] initWithCapacity:tempArray2.count];
for (NSDictionary *dict in tempArray) {
Commander *commander = [[Commander alloc] init];
[commander populateWithDictionary:dict];
for (NSNumber *uid in commander.abilitiesUIDArray) {
NSLog(@"uid %@", uid);
for (Ability *ability in self.germanAbilitiesArray) {
NSLog(@"ability uid is : %@, target uid %@ ",ability.uid, uid);
if ([ability.uid isEqualToNumber: uid]) {
NSLog(@"Adding abilty %@ to commander %@: ",ability.name, commander.name);
[commander.abilitiesArray addObject:ability];
NSLog(@"Current commander abilty array is %@: ",commander.abilitiesArray);
}
}
}
[tempCommandersArray addObject:commander];
}
self.germanCommandersArray = [NSArray arrayWithArray:tempCommandersArray];
NSLog(@"Final german Commaders %@",self.germanCommandersArray);
#pragma Soviet commander itertation
for (NSDictionary *dict in tempArray2) {
Commander *commander = [[Commander alloc] init];
[commander populateWithDictionary:dict];
for (NSNumber *uid in commander.abilitiesUIDArray) {
NSLog(@"uid %@", uid);
for (Ability *ability in self.sovietAbilitiesArray) {
NSLog(@"ability uid is : %@, target uid %@ ",ability.uid, uid);
if ([ability.uid isEqualToNumber: uid]) {
NSLog(@"Adding abilty %@ to commander %@: ",ability.name, commander.name);
[commander.abilitiesArray addObject:ability];
NSLog(@"Current commander abilty array is %@: ",commander.abilitiesArray);
}
}
}
[tempCommandersArray2 addObject:commander];
}
self.sovietCommandersArray = [NSArray arrayWithArray:tempCommandersArray2];
NSLog(@"Final Soviet Commaders %@",self.germanCommandersArray);
}
添加图像: