2

I am saving data from a server url to nsuserdefaults as User Detail.

Now at certain point, I want to update 2 keys of User Detail but i am unable to do that. No change occurs.

Here is my code:

  NSDictionary *user;
  NSMutableDictionary *userMut; 
  user=[[NSUserDefaults standardUserDefaults] objectForKey:@"User Detail"];
  userMut=[user mutableCopy];
  [userMut setValue:@"1" forKey:@"isMobileVerified"];
  [userMut setValue:mobileNo.text forKey:@"mobile"];

Any help would be appreciated! Plz let me know how to update key values in a dictionary stored in nsuserdefaults if i am doing it wrong.

4

3 回答 3

6

您需要在设置值后同步您的更改:

[[NSUserDefaults standardUserDefaults] setObject:userMut forKey:@"User Detail"];
[[NSUserDefaults standardUserDefaults] synchronize];
于 2013-11-14T10:31:13.113 回答
2

完成使用后尝试将值添加回来:

[[NSUserDefaults standardUserDefaults] setObject:userMut forKey:@"User Detail"];

您正在复制您的字典,然后对该副本不执行任何操作

于 2013-11-14T10:28:01.317 回答
2

您已经制作了字典的副本并对其进行了更改,这不会影响用户默认对象中的原始副本。

将字典设置回用户默认对象应该可以解决问题:

[[NSUserDefaults standardUserDefaults] setObject:userMut forKey:@"User Detail"];
于 2013-11-14T10:28:40.620 回答