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.
Related
As I am storing one Integer value into NSUserDefaults aas its used at many places. At first time its working fine but while i close my application and again open it I am check that user already selected any option in past feom NSUserDefaults stored value but I am failed in that
Some thing is wrong in my case
Here is my code for checking Integer value is there in userdefault :
NSInteger selectedBusinessUnit = [[NSUserDefaults standardUserDefaults] integerForKey:#"selectedUnit"];
if ( selectedBusinessUnit != NSNotFound){
//go to direct main screen.
}else {
// load Business unit screen for selection.
}
But its always found value even i am deleting app and reinastall it.
Always my selected value is 0.
Let me know what is my silly mistake here.
Edit:
[[NSUserDefaults standardUserDefaults] setInteger:sender.tag forKey:#"selectedUnit"];
[[NSUserDefaults standardUserDefaults] synchronize];
THNAKS .
Obviously there is a misunderstanding: NSNotFound is not equal to key is missing, it's a valid integer value.
The easiest way to keep your logic is to register the key-value pair with NSNotFound as the default value.
As soon as possible (applicationDidFinishLaunching or earlier) write
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *defaultValues = #{#"selectedUnit": #(NSNotFound)};
[defaults registerDefaults:defaultValues];
That means NSNotFound is considered as default value until it's overwritten the first time. The 3 lines must be executed every time the application launches. If the app is reinstalled the default value is taken again.
Now you can use your logic in the question.
PS: You don't need to synchronize after writing. The framework does that periodically.
The default value for integer is 0, according to the documentation:
-integerForKey: is equivalent to -objectForKey:, except that it converts the returned value to an NSInteger. If the value is an
NSNumber, the result of -integerValue will be returned. If the value
is an NSString, it will be converted to NSInteger if possible. If the
value is a boolean, it will be converted to either 1 for YES or 0 for
NO. If the value is absent or can't be converted to an integer, 0 will
be returned.
if you want to check if a key exists in NSUserDefaults use:
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"selectedUnit"] != nil)
{
...
}
NSUserDefaults storage only object, NSNumbers.
I think the problem in conversion from NSNumber, which you stored to integer in method integerForKey.
Another case - may be you forgot a synchronize NSUserDefaults.
You can set object like
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:#(yourIntegerValue) forKey:#"yourIntegerKey"];
[userDefaults synchronize];
and you can check like this
NSNumber *integerValue = [userDefaults objectForKey:#"yourIntegerKey"];
if (integerValue) {
//
}
I have strange problem with NSUserDefaults.
I would like to save NSMutableArray.
I have this piece of code:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:selected forKey:#"markListArr"];
The problem is that the object for key "markListArr" doesn't get saved.
I do this at the first run of the app.
When later in the app I want to save object for that key everything works fine.
If I use any other key everything works fine. I would like to use that specific key because I already have app on app store and this is only update to the existing app.
I already tried [userDefaults synchronize] and it doesn't work.
Any ideas what is happening?
Are you calling [userDefaults synchronize] before fetching the object? I'm using the code below:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *selected = [[NSMutableArray alloc] init];
[selected addObject:#"New Value"];
[userDefaults setObject:selected forKey:#"markListArr"];
[userDefaults synchronize];
NSMutableArray *arr = [userDefaults objectForKey:#"markListArr"];
NSLog(#"arr: %#", arr);
and it prints:
arr: (
"New Value"
)
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
I am trying to store userid coming from server to userDefaults which is a NSUserDefault object, but its always showing 0 for the value of userDefaults, I had tried using setObject method too but still its not working, and i have checked for informatioon coming from server its never a 0. value for user id is never 0 but still NSUserDefault is showin a 0 for it.
int userid=[[userdata objectForKey:#"id"] integerValue];
[userDefaults setInteger:userid forKey:#"UserID "];
[userDefaults synchronize];
NSLog(#"UID: %d",[[NSUserDefaults standardUserDefaults] integerForKey:#"UserID"]);
please help me thanks.
You are saving using the key #"UserId " note the space and reading using the key #"UserId" without an extra space.
As David Rönnqvist pointed out, you have a space in one of your keys but not in the other.
What I usually do is to #define all my user defaults keys in a file constants.h, and then use those constants instead of re-typing the string literal each time. This makes mistakes like yours impossible, and also makes it easier to keep track of the keys you are using.
you can try this way to store and retrieve data..
for set the value..
[[NSUserDefaults standardUserDefaults]setInteger:10 forKey:#"USERID"];
for retrieve the value..
NSLog(#"%d",[[[NSUserDefaults standardUserDefaults] valueForKey:#"USERID"]integerValue]);
good luck..
You can try this way
int userid=[[userdata objectForKey:#"id"] integerValue];
[userDefaults setInteger:userid forKey:#"UserID"];
[userDefaults synchronize];
NSLog(#"UID: %d",[[NSUserDefaults standardUserDefaults] integerForKey:#"UserID"]);
Happy coding........
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.