NSNotFound not working with NSUser default - ios

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) {
//
}

Related

ios/objective-c: Testing for existence of NSUserDefault

I wish to test whether an NSUserDefault value has been defined and if not define it. However, in the case where the value of the variable is 0 which I think is the same as NO, the following is resolving to TRUE and resetting the variable.
if(![[NSUserDefaults standardUserDefaults] boolForKey:#"notifyOn"]){
[prefs setBool:YES forKey:#"notifyOn"];
}
In other words, ![[NSUserDefaults standardUserDefaults] boolForKey:#"notifyOn"] appears to be true if the value is 0.
I only want to set the variable if it has not been previously set.
As an aside, when I log this to the console, I am viewing 1 and 0 instead of YES and TRUE. Am assuming they are the same, but if not, please confirm.
Thanks for help with this.
All values, including primitive types like BOOL, are actually stored as objects (an NSNumber object, in the case of BOOL) and objects can be nil, meaning "not set".
Therefore:
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"notifyOn"] == nil) {
// Value is neither YES or NO
// (same as setObject:#(YES) forKey:#"notifyOn")
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"notifyOn"];
}
as for 0 and 1 yes they are equivalent to NO and YES. So you are following the right approach.
If you want to check the existence of boolean value then [userDefaults boolForKey:#"theKeyOfMyBOOL"]; returns a BOOL, so either YES or NO (not nil).
Internally, it is stored as an NSNumber. So, if you call
[userDefaults objectForKey:#"theKeyOfMyBOOL"];
you will be given an NSNumber, if you have ever stored anything, or nil, if you have not.

NSInteger losing value when saved in NSUserDefaults

When the following code executes, it logs "0" even though the indexPath.row selected is "1".
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger location = indexPath.row;
[userDefaults setInteger:location forKey:#"savedlocations"];
NSInteger location2 = [userDefaults integerForKey:#"savedlocation"];
NSLog(#"l: %ld", location);
You've got several things that need to be fixed:
You're saving with the key savedlocations, but retrieving it with savedlocation. Note that the first is plural.
You're logging location, not location2, which is what's being pulled from NSUserDefaults. location2 will always be 0, because of the point above.
Edit: I had a third point here about calling synchronize, but it turns out that that is irrelevant to this situation, as explained by rmaddy in the comments below.
Other than those quick fixes, though, your configuration ought to work.
You are looking for the key "savedlocation" instead of "savedlocations".
EDIT:
0 is being returned by the method, which is what is returned for an NSInteger that can't be found. My bad on the weird wording.

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.

registerDefaults method sets the default value not the actual value.?

I want to understand the functionality of,
[NSUserDefaults standardUserDefaults] registerDefaults:];
Does registerDefaults method set the default value and not the actual value. For example, assuming i execute the following statements in sequence,
[[NSUserDefaults standardUserDefaults] setObject:#"1" ForKey:#"test"];
and then call,
[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:#"2",#"test", nil]];
I see that valueForKey test is still 1 and not 2.
Could someone provide me a better understanding of registerDefaults or is my above anology right?
If my anology is right,then does it mean that the default value is picked up only when no object is set for that specific key?
Thanks in advance.
Does registerDefaults method set the default value and not the actual value
Yes. The value that you pass in the dictionary to registerDefaults: is returned to you only when there is no specific value set for that key.
When you set the value #"1" for the key #"test", you "hide" the default value registered in the registerDefaults: call.
If you do this
[NSUserDefaults standardUserDefaults] registerDefaults:#{#"a":#"1", #"b":#"2"}, nil]];
[[NSUserDefaults standardUserDefaults] setObject:#"200" ForKey:#"b"]; // Override
and then request the values for keys #"a" and #"b", you get the default value #"1" for #"a" and the specific value of #"200" for #"b".

NSUser default is not saving any value

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........

Resources