Xcode: I Can't Set an NSUserdefault to itself + 5 - ios

This is for iOS
[[NSUserDefaults standardUserDefaults] integerForKey:#"money"] =
[[NSUserDefaults standardUserDefaults] integerForKey:#"money"] + 5;
Could Someone Please Help Me With This, I'm Getting This Error:
Assigning to 'readonly' return result of an Objective-C message not allowed

integerForKey is a getter method. used setInteger setter method for setting property like as bellowed.
[[NSUserDefaults standardUserDefaults] setInteger:[[NSUserDefaults standardUserDefaults] integerForKey:#"money"] + 5 forKey:#"money"];
Hope this help you.

Related

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.

Reading String With NSUserDefaults not working

I'm using iOS9 SDK, and I'm trying to read a string using :
NSString *savedID = [[NSUserDefaults standardUserDefaults] stringForKey:#"ID"];
I saved it using :
[[NSUserDefaults standardUserDefaults] setObject:[myPeripheral.identifier UUIDString] forKey:#"ID"];
[[NSUserDefaults standardUserDefaults] synchronize];
The problem is that the property savedID has a nil value.
When I execute the ligne bellow in the consol, I get the value I saved :
po [[NSUserDefaults standardUserDefaults] stringForKey:#"ID"];
PS : The value I saved is : 73BAE46D-3A74-E7F4-05F7-EFCB6AACC20C
I also tried to use objectForKey, but I still got the same result.
Any Idea?
I was debugging on a device, I tried to execute the App without debug and it worked :p
Thank you every one.

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

Save a value of a local variable [duplicate]

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

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