View Full Version : How to enumerate and clear all my app's preferences?
gameplayerxp
Aug 20, 2011, 09:11 PM
I want to do this in my app when users click a button. Is it possible? Thanks.
ArtOfWarfare
Aug 20, 2011, 09:58 PM
Yes
...
I feel silly saying just that, but I can't imagine what else to add to it.
gameplayerxp
Aug 21, 2011, 02:26 AM
Yes
...
I feel silly saying just that, but I can't imagine what else to add to it.
Thanks. Glad to know. But my question was HOW?
ArtOfWarfare
Aug 21, 2011, 05:55 AM
Store your preferences in an NSDictionary or NSMutableDictionary, and set up an IBAction on your button that looks something like
- (IBAction)clearPreferences:(id)sender {
[preferences enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
/* The code that actually clears each preference... you might want
to do something different for each preference based on its data type...
like, set NSStrings to @"", and NSNumbers to 0...
it depends on what "clear" is in your app.
For example:*/
if ([obj isMemberOfClass:[NSString class]]) obj = @"";
}
}
Hope that helps.
Sorry for the misunderstand... your post only said "Is it possible?", I didn't realize that in the title you'd actually asked for an example.
gameplayerxp
Aug 21, 2011, 08:04 AM
Store your preferences in an NSDictionary or NSMutableDictionary, and set up an IBAction on your button that looks something like
- (IBAction)clearPreferences:(id)sender {
[preferences enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
/* The code that actually clears each preference... you might want
to do something different for each preference based on its data type...
like, set NSStrings to @"", and NSNumbers to 0...
it depends on what "clear" is in your app.
For example:*/
if ([obj isMemberOfClass:[NSString class]]) obj = @"";
}
}
Hope that helps.
Sorry for the misunderstand... your post only said "Is it possible?", I didn't realize that in the title you'd actually asked for an example.
That's ok. I also noticed that I just put the right question on the topic but not in the content.
Thanks for your example. It is a bit different to my case. I am writing a reader app and some preferences are based on the book names. So these preferences are not fixed and I cannot know what they are when I am writing the code.
But your example reminds me that I can save all preference key names in a unique & global one and rebuild the dictionary from it when I need to delete them.
It's not very convenient. So really curious if there is an easier - just one function call - to do all of these. Like on Windows, if I save the preference in a file or registry, just need to delete the file or the whole reg key. That's it.
jiminaus
Aug 21, 2011, 04:21 PM
NSUserDefaults supports dictionary values. You could store your per-book preferences in a dictionary, then store that dictionary using NSUserDefaults with a key unique to the book. You can jettison that book's preferences by using removeObjectForKey: to remove the dictionary.
gameplayerxp
Aug 22, 2011, 05:29 AM
NSUserDefaults supports dictionary values. You could store your per-book preferences in a dictionary, then store that dictionary using NSUserDefaults with a key unique to the book. You can jettison that book's preferences by using removeObjectForKey: to remove the dictionary.
That's a good idea. Thanks:)
gameplayerxp
Aug 23, 2011, 03:22 AM
Finally found this: CFPreferencesCopyKeyList. I now can use it to get all preference keys and delete them one by one :)
vBulletin® v3.8.6, Copyright ©2000-2013, Jelsoft Enterprises Ltd.