nsuserdefault has key-values set how to delete it? - ios

I have already tried to delete all the key-values pair when the app starts and again when i check keys these keys are saved in NSUserDefault. I have these preferences stored.
NSArray *keys = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys];
NSLog(#"all keys %#", keys);
keys (
NSLanguages,
AppleITunesStoreItemKinds,
AppleLocale,
AppleLanguages,
NSInterfaceStyle
)
When I store new dynamic values to the NSUserDefaults, I want to select all the keys except these preferences.
Please help me with this problem.
Thanks in advance

Don't do what you're trying to do. Don't tamper with any of the keys added by Apple. Keep your own set of keys (preferably with prefixes on the names) and edit only those.
Generally you shouldn't delete keys unless there is some specific requirement in your app to do so. What you should do is to set default values for each of your keys when the app starts:
[[NSUserDefaults standardUserDefaults] registerDefaults:#{... : ...}];
These defaults will be valid while the app is running but won't be saved. If you set anything using any of the set...:forKey: methods and synchronize then they will overwrite the defaults and be saved.

Try this
- (NSDictionary *) dictionaryRepresentation. Using this method on the standard user defaults, you can get a list of all keys in the user defaults. You can then use this to clear the user defaults:
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict) {
[defs removeObjectForKey:key];
}
[defs synchronize];
removeObjectForKey -- that should give you the ability to remove a preference.

You can remove everything in NSUserDefaults associated with your app by calling this.
// remove entire user defaults
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
[[NSUserDefaults standardUserDefaults] synchronize]; // not sure if needed, but never hurts
This should give you a clean slate for saving information to user defaults.

Related

Remove NSDictionary from NSUserdefaults

I would like to remove an NSDictionary that is stored in NSUserdefaults.
I tried with this :
[NSUserDefaults removeObjectForKey:#"bookmarks"];
But, all my favorites are removed
How can I delete one single favorite?
I found the same question here
- Remove object of NSUserDefault by tableview cell
thanks all
Get the dictionary of bookmarks, creating a mutable copy, so you can modify it:
NSMutableDictionary *bookmarks = [[userDefaults objectForKey:#"bookmarks"] mutableCopy];
Remove the one you want:
[bookmarks removeObjectForKey:#"Bookmark be gone"];
Put the bookmarks back:
[userDefaults setObject:bookmarks forKey:#"bookmarks"];
and sync:
[userDefaults synchronize];
EDIT Following a comment from the OP, it sounds like it might be in an array, rather than a dictionary. The principle is the same:
Get the array of bookmarks, creating a mutable copy, so you can modify it:
NSMutableArray *bookmarks = [[userDefaults objectForKey:#"bookmarks"] mutableCopy];
Remove the one you want (to find the index of the object you probably need to iterate the array. See this question):
[bookmarks removeObjectAtIndex:5];
Put the bookmarks back:
[userDefaults setObject:bookmarks forKey:#"bookmarks"];
and sync:
[userDefaults synchronize];

Clear data of a particular NSUserDefaults object Data

In my project I am using NSUseDefaults for store data with the different objects.
NSUserDefaults *defaults1=[NSUserDefaults standardUserDefaults];
//---- I have set object for this
[defaults1 synchronize];
NSUserDefaults *defaults2=[NSUserDefaults standardUserDefaults];
//---- I have set object for this
[defaults2 synchronize];
Now I want clear all keys data only for defaults2, not for defaults1. So whenever I am applying below code:
NSDictionary *defaultsDictionary = [defaults2 persistentDomainForName: appDomain];
for (NSString *key in [defaultsDictionary allKeys]) {
NSLog(#"removing user pref for %#", key);
[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}
Above code have remove value for defaults2 but also for defaults1. But I don't want to remove objects for defaults1. So please help me out.
NSUserDefaults is like a singelton class so it will always return the same shared system object.
You can store multiple objects using multiple keys and can delete/remove objects against those keys.
If you have read a doc about NSUserDefaults standardUserDefaults you should know that standardUserDefaults Returns the shared defaults object. and actually defaults1 and defaults2 the same.
You can store keys and then delete only those keys like:
NSUserDefaults *defaults1=[NSUserDefaults standardUserDefaults];
//---- I have set object for this
[defaults1 synchronize];
[[defaults1 dictionaryRepresentation] allKeys]; // use this keys for deleting

Finding recurring items in NSUserDefaults

I am having a hard time understanding how to go about finding the values that repeat more than once. I'm just not understanding from the code below how to go about getting the duplicate values. thanks for any help!!
[appDelegate.scannedNumbers addObject:result];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:appDelegate.scannedNumbers forKey:#"scannedNumbers"];
[userDefaults synchronize];
[BT_debugger showIt:self message:[NSString stringWithFormat:#"After writing scannedNumbers: %#" [[NSUserDefaults standardUserDefaults] objectForKey:#"scannedNumbers"]]];
Setting a default has no effect on the value returned by the objectForKey: method if the same key exists in a domain that precedes the application domain in the search list.
Apple Documentation
You have to check if the key already exists before you will set another object for the same key.

NSUserdefaults multiple data types for one key

After reading from various answers I have come to know that NSUserDefaults can save multiple datatypes for one key. But what I cannot find is if
[NSUserDefaults standardUserDefaults] removeObjectForKey:"someKey"];
removes all objects of all data types associated with that key?
You cannot store different kind of objects for one key.
If you set an object for a key it will erase the old one.
But, if your are searching for a way to store multiple data for one key, you can store a NSDictionary.
Ex :
MyObject *obj = [[MyObject alloc] init];
NSString *otherType = #"mystring";
NSDictionary *multipleData = #{ #"key1" : obj , #"key2" : otherType}
[[NSUserDefaults standardUserDefaults] setObject: multipleData forKey:#"multipleData"];
[[NSUserDefaults standardUserDefaults] synchronize];
And if you want to remove it :
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"multipleData"];
[[NSUserDefaults standardUserDefaults] synchronize];
Yes it does.
Your data may be anything an array or dictionary or simple int. This command will remove that data.
As iPatel suggested. You need to call:
[[NSUserDefaults standardUserDefaults] synchronize];
After adding or deleting any data. Hope this helps.. :)
You cannot store multiple objects under one key. NSUserDefaults acts just like a NSDictionary. When you set an object for a specific key you overwrite the old object. So removeObjectForKey: just removes one object/value; the one you had stored under that key.
Do you call
[[NSUserDefaults standardUserDefaults] synchronize];
after delete all the data of the key and also might be you can not store multiple data on single key, it's return new one that inserted to last. ?
Read official documentation of removeObjectForKey of NSUserDefaults.
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSString *yourDomain = [[NSBundle mainBundle] bundleIdentifier];
[userDefault removePersistentDomainForName:yourDomain];
Here. if u want to reset.

Add object to existing key in NSuserDefault

I want to add the another object to existing key in NSUserDefault.
I try this code to add in NSUserDefault.
delegate.str=[first objectAtIndex:delegate.selectId];
NSLog(#"%#",delegate.str);
delegate.str1=[details1 objectAtIndex:delegate.selectId];
NSLog(#"%#",delegate.str1);
[delegate.BookMarkDefault setObject:delegate.str forKey:#"Name"];
// BookMarkDefault This is NSUSERDefault.
First I will insert delegate.str value value successfully goes to NSUSerDefault, but when next time adding new value through delegate.str for key under Name will replace previous value. I want to add new value under the same Key.
This is not possible if you want to fullfill this kind of requirement then try to use array and then put that array into NSUSerDefault...
Try this code
delegate.str=[first objectAtIndex:delegate.selectId];
NSLog(#"%#",delegate.str);
delegate.str1=[details1 objectAtIndex:delegate.selectId];
NSLog(#"%#",delegate.str1);
[yourarray addObject:delegate.str];
NSLog(#"%#",delegate.Bookmarknamearray);
[delegate.BookMarkDefault setObject:youarray forKey:#"Name"];
No its not possible to assign multiple values to same key. If you want to use multiple values to same key then use array or dictionary and assign them to the key.
Ex :
[[NSUserDefaults standardUserDefaults] setObject:YOUR_ARRAY forKey:#"Name"];
Hope it helps you.
You can't save two objects for same key.In NSUserDefaults and NSDictionary keys will be unique (There can be only one object for a particular key).
If you need to do this, you can store the values in NSMutableArray or NSDictionary and add that to the NSUserDefaults like:
[[NSUserDefaults standardUserDefaults] setObject:dataArray forKey:#"Name"];
It may be helpful
[[NSUserDefaults standardUserDefaults] setObject: delegate.str forKey:#"Name"];
You can not assign two value in single key of NSUserDefaults, you must need to take another "KEY" for each value . your can replace pervious value by using following code :
Just try with following code :
[[NSUserDefaults standardUserDefaults] setObject: delegate.str forKey:#"Name"];
EDITE:
But you can add array in NSUserDefaults, suchlike
self.golbalArr = [[NSMutableArray alloc]initWithObject:#"1", #"2",....,nil];
[[NSUserDefaults standardUserDefaults] setObject: self.golbalArr forKey:#"Name"];

Resources