在頭文件:如何使用setValue或setObject修改NSMutableDictionary?
@property (strong, nonatomic) NSMutableArray *vocabs;
@property (strong, nonatomic) NSMutableDictionary *vocab;
在.m文件
:
-(void) loadFile {
NSString* filepath = [[NSBundle mainBundle]pathForResource:@"vocabs" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:filepath];
vocabs = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
}
-(void) renderVocabs {
//NSLog(@"json file = %@", vocabs);
if ([vocabs count] == 0) {
} else {
vocab = [vocabs objectAtIndex:vocabIndex];
//NSLog(@"%d", vocabIndex);
//NSLog(@"%d", [vocabs count]);
NSString *word = [vocab objectForKey:@"word"];
labelWord.text = word;
tvDefinitions.text = [NSString stringWithFormat:@"(%@) %@" , [vocab objectForKey:@"subject"], [vocab objectForKey:@"definitions"]];
NSString *imgName = [NSString stringWithFormat:@"%@.jpg",word];
NSLog(@"%@", imgName);
[imageVocab setImage: [UIImage imageNamed:imgName]];
NSString *remembered = [vocab objectForKey:@"remembered"];
if ([remembered isEqualToString:@"0"]) {
self.btnRemember.hidden = FALSE;
} else {
self.btnRemember.hidden = TRUE;
}
[self setDisplayFontSize];
}
}
- (IBAction)btnTick:(UIButton *)sender {
[vocab setObject:@"1" forKey:@"remembered"];
}
和我
** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
*** First throw call stack:
我做了什麼錯?任何人都可以指向正確的方向嗎?提前致謝。
的錯誤是明顯的。 'vocab'實際上是一個'NSDictionary',而不是'NSMutableDictionary'。 – rmaddy
是的,我的意思是setObject – tipsywacky
顯示如何初始化vocab,可能在那裏你只有一個不可變的'NSDictionary' – Volker