Save a value of a local variable [duplicate] - ios

This question already has answers here:
Saving application data state on exit
(3 answers)
Closed 8 years ago.
How can I save a value of a variable in objective-c, for example a BOOL, and when I return for the app the value of my variable is the last value setted?! For exemple:
BOOL *firstAccess;
firstAccess = TRUE;
I finish the app and return, and i wished that my variable were TRUE, again.
Thanks

You can use NSUserDefaults to save data permanently:
BOOL firstAccess;
/// ...
[[NSUserDefaults standardUserDefaults] setBool:firstAccess forKey:#"FIRSTACCESS"];
To retrieve the value:
firstAccess = [[NSUserDefaults standardUserDefaults] boolForKey:#"FIRSTACCESS"];
See the documentation for NSUserDefaults for more information.

You can store the BOOL value in NSUserDefaults:
#define MYFirstAccessKey #"firstAccessKey"
+ (void)initialize {
// Set the default value. This is returned if nothing has been written for the key.
[[NSUserDefaults standardUserDefaults] registerDefaults:#{ MYFirstAccessKey : #YES }];
}
- (void)someMethod {
// Read the value.
BOOL firstAccess = [[NSUserDefaults standardUserDefaults] boolForKey:MYFirstAccessKey];
if (firstAccess) {
// ... do stuff ...
}
// ... later ...
// Set value.
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:MYFirstAccessKey];
[[NSUserDefaults standardUserDefaults] synchronize]; // Write to disk now.
}

Related

Synch NSUserDefaults value on language change and compare

EDIT: On change language to Hindi...i want to change the value of saved NSUserdefault as well! how do i do it?
I have create a singleton class which save the value for e.g.:
singleton.userStatus=NSLocalizedString(#"yes", nil);
I have set
"yes" = #"YES";
in hindi
"yes" = #"हाँ";
in my NSLocalizedString
and
[[NSUserDefaults standardUserDefaults] setObject:singleton.userStatus forKey:#"USER_STATUS"];
Now i am comparing it
NSString *checkStatus = [[NSUserDefaults standardUserDefaults] objectForKey:#"USER_STATUS"]; // i have set its default value YES
if ([checkStatus isEqualToString:NSLocalizedString(#"yes", nil)])
{
// not comes here
}
else
{
//
}

call different method with same string in different value

I have two methods in my .m file .And i want to access by different values but in my Facebook variable gives nil value but if i use only one line and remove second line of object for key then it work fine for one method .
How i do this which work for my both methods
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[[NSUserDefaults standardUserDefaults] synchronize];
user=[defaults objectForKey:#"userid"];
facebook=[defaults objectForKey:#"FACEBOOKprofile"];
facebook =[defaults objectForKey:#"VLCC"];
if ([facebook isEqualToString:#"VLCCFACEBOOK"])
{
[self FacebookRecord];
}
else if([facebook isEqualToString:#"VLCC"])
{
[self VlccRecord];
}
assume that
Choice-1
// for accessing the both condition in same time
Initially Store the UserDefault value based on your method.
if you are access with facebook , on that time store the string like
[[NSUserDefaults standardUserDefaults] setObject:"VLCCFACEBOOK" forKey:#"FACEBOOKprofile"];
[[NSUserDefaults standardUserDefaults] synchronize];
if you are access with VLCC , on that time store the string like
[[NSUserDefaults standardUserDefaults] setObject:"VLCC" forKey:#"VLCCprofile"];
[[NSUserDefaults standardUserDefaults] synchronize];
and retrieve the both and check like
if ([[[NSUserDefaults standardUserDefaults]
objectForKey:#"FACEBOOKprofile"]isEqualToString:#"VLCCFACEBOOK"])
{
[self FacebookRecord];
}
if([[[NSUserDefaults standardUserDefaults]
objectForKey:#"VLCCprofile"] isEqualToString:#"VLCC"])
{
[self VlccRecord];
}
Choice-2
// for accessing single condition on single time
if you are access with facebook , on that time store the string like
[[NSUserDefaults standardUserDefaults] setObject:"VLCCFACEBOOK" forKey:#"FACEBOOKprofile"];
[[NSUserDefaults standardUserDefaults] synchronize];
if you are access with VLCC , on that time store the string like
[[NSUserDefaults standardUserDefaults] setObject:"VLCC" forKey:#"FACEBOOKprofile"];
[[NSUserDefaults standardUserDefaults] synchronize];
and retrieve the both and check like
if ([[[NSUserDefaults standardUserDefaults]
objectForKey:#"FACEBOOKprofile"]isEqualToString:#"VLCCFACEBOOK"])
{
[self FacebookRecord];
}
else if([[[NSUserDefaults standardUserDefaults]
objectForKey:#"FACEBOOKprofile"] isEqualToString:#"VLCC"])
{
[self VlccRecord];
}
In your code, the [self VlccRecord] method will always get called because you are overwriting the facebook variable.
facebook=[defaults objectForKey:#"FACEBOOKprofile"];
facebook =[defaults objectForKey:#"VLCC"];
If you are looking to append the two strings : Then use the stringbyAppendingString method.
int a = 10;
a = 20;
print >> a; //20
int a = 10;
print1 >> a; //10
a = 20;
print2 >> a; //20
Here,
a = facebook;
print1 = [self FacebookRecord];
print2 = [self VlccRecord];
I believe you'll understand.

How to store BOOL in NSUserDefaults [duplicate]

This question already has answers here:
iOS: Use a boolean in NSUserDefaults
(6 answers)
Closed 7 years ago.
view and viewcontroller are separate. I am already tried NSUserDefaults it's not working.
[[NSUserDefaults standardUserDefaults] setValue:BOOLCondition forKey:#"YES"];
[[NSUserDefaults standardUserDefaults] synchronize];
it's say impact conversion of bool to id is disallowed with arc
how to pass that bool value
***** view to viewcontroller not viewcontroller to viewcontroller
Use the below code to store BOOL value in NSUserDefaults
BOOL BOOLCondition = YES; or BOOL BOOLCondition = NO;
[[NSUserDefaults standardUserDefaults] setBool:BOOLCondition forKey:#"YES"];
[[NSUserDefaults standardUserDefaults] synchronize];
I always do this to store a BOOL value, because I prefer better to work objects directly:
BOOL _myBool = TRUE; // or FALSE...
[[NSUserDefaults standardUserDefaults] setValue:#(_myBool) forKey:#"boolValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
and restoring back the stored value:
BOOL _boolValue = [[[NSUserDefaults standardUserDefaults] valueForKey:#"boolValue"] boolValue]; // will be FALSE if the value is `nil` for the key

`NSUserDefaults` not saving float properly

I am building an application in which I want to save some user data by using NSUserDefaults, and it is a property of one of my controllers as following:
#property (nonatomic, strong) NSUserDefaults *userPreference;
#synthesize userPreference = _userPreference;
And I am trying to save something called a scale that should be a float, so I do something like this in the getter to put in some default values of scale in case user did not enter one:
if (!_userPreference) {
_userPreference = [NSUserDefaults standardUserDefaults];
}
if (![_userPreference floatForKey:#"scale"]) {
// if user has not entered this info yet
// also 0 is not a valid value so I can do this
[_userPreference setFloat:100.0 forKey:#"scale"]; // let default scale be 100.0
[_userPreference synchronize];
}
However, when I later on query this, no matter what I set in the default value, the following command:
[self.userPreference floatForKey:#"scale"];
always return 1. I am not sure what is happening. Am I not creating the NSUserDefaults correctly?
Thank you in advance!
if you want to use NSUserDefaults, do not create any variables or #propertys. Just use
[[NSUserDefaults standardUserDefaults] setFloat:forKey:]
[[NSUserDefaults standardUserDefaults] synchronize];
to save your data and
[[NSUserDefaults standardUserDefaults] floatForKey:]
for fetching data.
Store using
[_userPreference setObject:[NSNumber numberWithFloat:100.0] forKey:#"Scale"];
[_userPreference synchronize];
and retrieve it using
float value = [[_userPreference objectForKey:#"Scale"] floatValue];
Have you tried this to set the float value:
[_userPreference setObject:[NSNumber numberWithFloat:100.0f] forKey#"scale"];
and the following to retrieve:
CGFloat scale = [[_userPreference objectForKey#"scale"] floatValue];
Change your if condition to this:
if ([_userPreference floatForKey:#"scale"] == 0.0f ) {
NSUserDefaults will always return 0.0f if no value is set for the key. Since you are saying that "also 0 is not a valid", you can safely use this approach
try this
-(void) saveFloatToUserDefaults:(float)x forKey:(NSString *)key {
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setFloat:x forKey:key];
[userDefaults synchronize];
}
-(float) retriveFromUserDefaultsForKey:(NSString *)key {
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
return [userDefaults floatForKey:key];
}
[self saveFloatToUserDefaults:6.55 forKey:#"myFloat"];
float x = [self retriveFromUserDefaultsForKey:#"myFloat"];

NSUserDefaults doesn't save my values

I use this code to save:
[[NSUserDefaults standardUserDefaults] setValue:#"vruch" forKey:#"nastoikaIP"];
[[NSUserDefaults standardUserDefaults] synchronize];
and this code to load:
NSString* automanual = [[NSUserDefaults standardUserDefaults]
valueForKey:#"nastroikaIP"];
if ([automanual isEqualToString:#"vruch"]) {
autovruch.selectedSegmentIndex = 1;
[self obnovittext];
}
You should not use the method setValue: (from the NSKeyValueCoding protocol) but setObject: (from the NSUserDefaults class) to store the string.
[[NSUserDefaults standardUserDefaults] setObject:#"vruch" forKey:#"nastroikaIP"];
[[NSUserDefaults standardUserDefaults] synchronize];
You can retrieve the string using the method stringForKey:
NSString* automanual = [[NSUserDefaults standardUserDefaults] stringForKey:#"nastroikaIP"];
Finally, note that it is generally better to define constants for the keys you use in NSUserDefaults to avoid any mistyping errors, which may be hard to debug when they happen.
Edit
It looks like you already did a mistyping error nastoikaIP != nastroikaIP. Notice the missing/extra r letter.
Your saving a string so you need to setObject rather than setValue
[[NSUserDefaults standardUserDefaults] setObject:#"vruch" forKey:#"nastoikaIP"];
[[NSUserDefaults standardUserDefaults] synchronize];
and change it for the loading aswell
NSString* automanual = [[NSUserDefaults standardUserDefaults] objectForKey:#"nastroikaIP"];
if ([automanual isEqualToString:#"vruch"]) {
autovruch.selectedSegmentIndex = 1;
[self obnovittext];
}

Resources