How to transfer new int value and save previous int value? - ios

I used
NSString *level =[[NSUserDefaults standardUserDefaults] stringForKey:#"Key"];
But this method may contain only one value.
How to transfer new int value and save previous int value?

A key can only have one value.
If you want to save a previous value, use a new key.
I'd recommend "oldKey" or "previousKey" in your user defaults to indicate the previous value. E.G.:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *oldLevel = [defaults stringForKey: #"Key"];
[defaults setObject: oldLevel forKey: #"PreviousKey"];
[defaults setObject: newLevel forKey: #"Key"];

Related

ios saving and retrieving NSNumber from NSUserdefaults

I've searched but I can't find a good explanation as to why I'm still not getting the right value from my saved Integer.
This is how I saved my event Id.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:eventSelected.eventId forKey:#"currentEvent"];
This is how I'm trying to retrieve it.
NSLog(#"user dfault %#", [NSNumber numberWithUnsignedInt:[[[NSUserDefaults standardUserDefaults] objectForKey:#"currentEvent"] unsignedIntegerValue]]);
I'm gettin gthis.
user dfault 123581200
Also, to note, event if I did intValue instead of unsignedIntegerValue , it would still give me a random id number. was wondering what I'm doing wrong.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// Convert your value to IntegerValue and Save it
[prefs setInteger:[eventSelected.eventId integerValue] forKey:#"currentEvent"];
// Dont forget to synchronize UserDefaults
[prefs synchronize];
// To Access this value
NSLog(#"%d",[prefs integerForKey:#"currentEvent"]);
Instead of this code NSLog(#"user dfault %#", [NSNumber numberWithUnsignedInt:[[[NSUserDefaults standardUserDefaults] objectForKey:#"currentEvent"] unsignedIntegerValue]]);
Use this
NSLog(#"user dfault %#", [[NSUserDefaults standardUserDefaults] integerForKey:#"currentEvent"]);
NSNumber give a pointer value, not integer value.
To retrieve the integer you should use below function:
[[NSUserDefaults standardUserDefaults] integerForKey:#"currentEvent"];
when you ask for objectForKey, it box it first and then return the object. Posting intValue will convert the address of that object into integer. Hence each time you will receive different number.
NSNumber *number;
store:
[[NSUserDefaults standardUserDefaults] setValue:number forKey:#"Number"];
retrieving:
number = [NSNumber numberWithInteger:[[NSUserDefaults standardUserDefaults] integerForKey:#"Number"]];

how to store slider values in ios

Hi I am using uislider and get the values in label. now i try to save the slider values. I don't know how to do this. If anybody knows please share the code. This is my code for slider changed.
-(IBAction)sliderChanged:(id)sender
{
UISlider *slider = (UISlider *)sender;
if((int)slider.value % 10 == 0)
{
sliderLabel.text = [[NSString alloc] initWithFormat:#"Value of:%d", (int)slider.value];
}
}
You can use NSUserDefault for storing the slider value.
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:sliderLabel.text forKey:#"Slider"];
[defaults synchronize];
You can retrieve the data like:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString *strValue = [defaults objectForKey:#"Slider"];
myLabel.text = strValue != nil ? strValue : #"No Value";
The above code is saving the sliderLabel to the NSUserDefault. If you want to store the slider value in the form of float then use:
[defaults setObject:[NSNumber numberWithFloat:slider.value] forKey:#"Slider"];
Retreive it like:
NSNumber *strValue = [defaults objectForKey:#"Slider"];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithFloat:slider.value] forKey:#"sliderValue"];
I believe you should store the value as a float (the original type for a UISlider value), it's just cleaner, and you'll probably want to reuse the float value to set back your slider when reopening the screen with something like:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[self.slider setValue:[[defaults objectForKey#"sliderValue"] floatValue]];

Set switch position to ON by first app launch using NSUserDefaults

I have switches in my app that stores position by standardUserDefaults, but when app starts for first time all my switches are in OFF position. How can I set them to ON by default?
Save
NSUserDefaults *defs1 = [NSUserDefaults standardUserDefaults];
[defs1 setBool: blackSwitch.on forKey: #"blackKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
Load
NSUserDefaults *defs1 = [NSUserDefaults standardUserDefaults];
blackSwitch.on = [defs1 boolForKey: #"blackKey"];
First of all
Do the following
NSUserDefaults *defs1 = [NSUserDefaults standardUserDefaults];
BOOL isOn = [defs1 boolForKey: #"blackKey"];
if(isOn) NSLog(#"isOn");
Second asure that blackSwitch is not nil
if(blackSwitch) NSLog(#"blackSwitch is not nil");
If blackswitch is nil, that means that you are still in early stage of view controller initialization, you should move the code that sets it on to viewDidLoad or viewWillAppear
For setting default values please use this code
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *testValue = [NSNumber numberWithBool:YES];
NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
testValue, #"blackKey", nil];
[defaults registerDefaults:appDefaults];
[appDefaults release];
Use the NSUserDefaults method registerDefaults to pass a default value of 'true' along with your #"blackKey" key. This will ensure that #"blackKey" is set to ON until the user makes a change to it themselves.

NSUserDefaults Contains Value Or Not?

How to know whether NSUserDefaults contains any value?How to check whether its empty?
There isn't a way to check whether an object within NSUserDefaults is empty or not.
However, you can check whether a value for particular key is nil or not.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSObject * object = [prefs objectForKey:#"your_particular_key"];
if(object != nil){
//object is there
}
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *string = [data objectForKey:#"yourKey"];
if(string==nil)
NSlog(#"nil")
Take a look at NSUserDefault documentation
// For saving the values
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// saving an NSString
[userDefaults setObject:#"Ttest" forKey:#"key"];
// --- For Retrieving
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [userDefaults stringForKey:#"key"];
To check whether a specific value is set or not, no matter of its location (global or application's), check the returned value of -[NSUserDefaults objectForKey:]
id obj = [[NSUserDefaults standardUserDefaults] objectForKey:#"My-Key-Name"];
if (obj != nil) {...}
To check if the application (bundle) has any settings stored in user defaults:
NSUserDefaults* sdu = [NSUserDefaults standardUserDefaults];
NSString* bundleId = [[NSBundle mainBundle] bundleIdentifier];
NSDictionary* mainBundleSettings = [sdu persistentDomainForName:bundleId];
NSLog(#"%#", mainBundleSettings);
If you are interested in all possible values for which -[NSUserDefaults objectForKey:] will return something, including system global settings, simply call
NSDictionary* allPossibleSettings = [sdu dictionaryRepresentation];
NSUserDefaults is never empty. It combines global settings, bundle's settings, temporary data and maybe something else. For example, if you call:
[[NSUserDefaults standardUserDefaults] objectForKey:#"NSBoldSystemFont"]
you will get the #"LucidaGrande-Bold" string value which will be taken from global settings, even when your application has never set this value.

How to get the nsuserdefault value in other viewcontrollers

I have two viewcontrollers. In my first viewcontroller:
{
NSString *string=textField.text;
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
[data setObject:string forKey:#"strings"];
[data synchronize];
}
How do I get the string value in my other viewcontroller?
Here you can use this in anyway in your application for store value of NSUserDefaults.
// --- Saving
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:#"TextToSave" forKey:#"keyToLookupString"];
// saving an NSInteger
[prefs setInteger:42 forKey:#"integerKey"];
// saving a Double
[prefs setDouble:3.1415 forKey:#"doubleKey"];
// saving a Float
[prefs setFloat:1.2345678 forKey:#"floatKey"];
// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];
Here you can use this in anyway in your application for get value of NSUserDefaults.
// --- Retrieving
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [prefs stringForKey:#"keyToLookupString"];
// getting an NSInteger
NSInteger myInt = [prefs integerForKey:#"integerKey"];
// getting an Float
float myFloat = [prefs floatForKey:#"floatKey"];
you can access NSUserDefaults in any controller (Any class of your application) of your application with the same code you have written in one class.
for getting the string value use the below code
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *myString = [data objectForKey:#"strings"];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSString *myString = [defaults stringForKey:#"strings"];
THis is the way to retrieve the data. Please note NSUserDefault is not used to pass data between two controllers. There are better methods for that.
Edit : After seeing Shaan Singh's comment
To pass data 2 view controllers you can declare a property in second view controller and access that from the present view controller.
It is already answered brilliantly here.
In the other ViewController,
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *string = [data objectForKey:#"strings"];
This will work because NSUserDefaults are a global key-value store that will persist across classes and applications.

Resources