我正試圖用NSUserDefaults
保存NSMutableDictionary
。我在stackoverflow上閱讀了很多關於這個主題的文章...我也發現了一個可以工作的選項;然而不幸的是,它只工作了一次,然後它開始只保存(空)。 有人有提示嗎?爲什麼NSUserDefaults無法保存NSMutableDictionary?
由於
代碼來保存:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:dictionary] forKey:@"Key"];
[[NSUserDefaults standardUserDefaults] synchronize];
代碼加載:
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"Key"];
dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
代碼以對象添加到NSMutableDictionary
:
[dictionary setObject:[NSNumber numberWithInt:0] forKey:@"Key 1"];
[dictionary setObject:[NSNumber numberWithInt:1] forKey:@"Key 2"];
[dictionary setObject:[NSNumber numberWithInt:2] forKey:@"Key 3"];
代碼以NSLog的( )值S:
for (NSString * key in [dictionary allKeys]) {
NSLog(@"key: %@, value: %i", key, [[dictionary objectForKey:key]integerValue]);
}
而且還鍵(空):
NSLog(@"%@"[dictionary allKeys]);
真的很好提出的問題! – zaph