Saving NSDictionary to NSUserDefaults

后端 未结 2 507
花落未央
花落未央 2020-12-13 08:50

I have a NSMutableDictionary, and i have added values to it (several key pair values). Now i require to save this NSMutableDictionary to a NSUserDefaults object.

1.

2条回答
  •  清歌不尽
    2020-12-13 09:20

    Your code is correct, but the one thing you have to keep in mind is that NSUserDefaults doesn't distinguish between mutable and immutable objects, so when you get it back it'll be immutable:

    NSDictionary *retrievedDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"DicKey"];
    

    If you want a mutable dictionary, just use mutableCopy:

    NSMutableDictionary *mutableRetrievedDictionary = [[[NSUserDefaults standardUserDefaults] objectForKey:@"DicKey"] mutableCopy];
    

提交回复
热议问题