Joh*_*eek 5 objective-c nsmutabledictionary
如何将多个对象添加到同一个键中NSMutableDictionary?我找不到合适的方法.该setObject方法仅更新密钥数组中的单个对象.该方法addObject: forkey:在NSMutableDictionary不可用,并导致崩溃的时候才使用.
从plist文件中读取字典.
词典:
temp = {
nickname : score
item 0 = level1;
item 1 = level2;
item 3 = level3;
score
item 0 = 400;
item 1 = 400;
item 3 = 400;
}
Run Code Online (Sandbox Code Playgroud)
这是代码:
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
str_nickname = [temp objectForKey:@"nickname"];
for (NSString *key in str_nickname){
if ([key isEqualToString:@"level2"]) { //replace object with new name
[newDict setObject:@"new level" forKey:@"nickname"];
} else {
[newDict addObject:key forKey:@"nickname"]; //wont work!!!
}
}
Run Code Online (Sandbox Code Playgroud)
另外,我想更新新词典中的新分数,并且必须在相应的级别对象上更新,也许是通过索引?
如果你的 plist 文件是这样的“test.plist”
The Dictionary temp={
nickname : Junaid Sidhu
levels
item 0 = level1;
item 1 = level2;
item 3 = level3;
score
item 0 = 400;
item 1 = 400;
item 3 = 400;
}
Run Code Online (Sandbox Code Playgroud)
这是代码
NSDictionary *temp = [[NSDictionary alloc]initWithContentsOfFile:[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"]];
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
NSString *str_nickname = [temp objectForKey:@"nickname"];
NSMutableArray *levels = [NSMutableArray new];
NSMutableArray *score = [NSMutableArray new];
for (NSString *key in [temp allKeys]){
if ( [key isEqualToString:@"levels"]) { //replace object with new name
levels = [NSMutableArray arrayWithArray:(NSArray *)[temp objectForKey:key]];
}
else if ( [key isEqualToString:@"score"]){
score = [NSMutableArray arrayWithArray:(NSArray *)[temp objectForKey:key]];
}
}
NSLog(@"Nick : %@ \n levels : %@ \n score : %@ \n",str_nickname,levels,score)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8354 次 |
| 最近记录: |