如何将用户默认设置再次设置为字典。
创建字典对象并为其设置值。
NSMutableDictionary * loginDictionary = [[NSMutableDictionary alloc] init];
[loginDictionary setValue:@"My Address" forKey:@"ADDRESS_KEY"];
[loginDictionary setValue:@"1234567890" forKey:@"PHONE_KEY"];
[[NSUserDefaults standardUserDefaults] setObject:newDictionary forKey:@"INFO_KEY"];
[[NSUserDefaults standardUserDefaults] synchronize];
现在我评估那些字典值并分配给其他一些对象。
NSDictionary *RetrievedDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"INFO_KEY"];
txt_Address.text = [RetrievedDictionary objectForKey:@"ADDRESS_KEY"];
txt_Mobile.text = [RetrievedDictionary objectForKey:@"PHONE_KEY"];
如何更新?
这就是我的尝试。
[[NSUserDefaults standardUserDefaults] setObject:@"Changed Address" forKey:@"ADDRESS_KEY"];
[[NSUserDefaults standardUserDefaults] setObject:@"0987654321" forKey:@"PHONE_KEY"];
[[NSUserDefaults standardUserDefaults] synchronize];
它不适合我,需要你的小建议我做错了什么。 @Thanks In Advance。
答案 0 :(得分:2)
NSMutableDictionary* changedDictionary = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"INFO_KEY"]];
[changedDictionary setObject:@"Changed Address" forKey:@"ADDRESS_KEY"];
[changedDictionary setObject:@"0987654321" forKey:@"PHONE_KEY"];
[[NSUserDefaults standardUserDefaults] setObject:changedDictionary forKey:@"INFO_KEY"];
[[NSUserDefaults standardUserDefaults] synchronize];
答案 1 :(得分:0)
NSMutableDictionary * loginDictionary = [[NSMutableDictionary alloc] init];
[loginDictionary setValue:@"My Address" forKey:@"ADDRESS_KEY"];
[loginDictionary setValue:@"1234567890" forKey:@"PHONE_KEY"];
[[NSUserDefaults standardUserDefaults] setObject:newDictionary forKey:@"INFO_KEY"];
[[NSUserDefaults standardUserDefaults] synchronize];
您正在创建loginDictionary,但是将newDictionary添加到NSUserDefaults。我还建议使用常量键。
答案 2 :(得分:0)
这个怎么样?
NSMutableDictionary *changedDictionary = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"INFO_KEY"] mutableCopy];
[changedDictionary setObject:txt_Address.text forKey:@"ADDRESS_KEY"];
[changedDictionary setObject:txt_Mobile.text forKey:@"PHONE_KEY"];
[[NSUserDefaults standardUserDefaults] setObject:changedDictionary forKey:@"INFO_KEY"];
[[NSUserDefaults standardUserDefaults] synchronize];