NSUserDefaults in NSMutableArray - iphone

How to set/make this into an NSMutableArray?
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:1 forKey:#"KeyI1"];
[prefs setInteger:1 forKey:#"KeyJ1"];
[prefs setInteger:1 forKey:#"KeyI2"];
[prefs setInteger:1 forKey:#"KeyJ2"];
Which I can use for an if statement something like this:
NSInteger I1=[prefs integerForKey:#"KeyI1"];
NSInteger I2=[prefs integerForKey:#"KeyI2"]
NSInteger J1=[prefs integerForKey:#"KeyJ1"]
NSInteger J2=[prefs integerForKey:#"KeyJ2"]
if ( I1 == index1 && K1 == index2){
}
if ( I2 == index1 && K2 == index2){
}

You can't use setInteger: for NSUserDefaults (which uses a NSMutableDictionary structure) directly. You should use setObject:forKey: and objectForKey: and add your integers in NSNumber objects or write a NSMutableDictionary category that will handle setInteger:forKey: and integerForKey:.

Related

Restoring value in NSUserDefault

I have stored my initial set up values using NSUserDefault like this...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:is_remember_chkd forKey:remember_me.titleLabel.text]; // Button text as key
[defaults setBool:is_signin_auto_chkd forKey:signin_automatic.titleLabel.text];
[defaults setBool:is_signin_secret_chkd forKey:signin_secret.titleLabel.text];
[defaults synchronize];
And I retrieved as ....
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
is_remember_chkd = [defaults objectForKey:remember_me.titleLabel.text];
is_signin_auto_chkd = [defaults objectForKey:signin_automatic.titleLabel.text];
is_signin_secret_chkd = [defaults objectForKey:signin_secret.titleLabel.text];
But I am not getting the last value, Am I doing anything wrong.
For retrieved the NSUserDefaults Value , replace the objectForKey to boolForKey .
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
is_remember_chkd = [defaults boolForKey:remember_me.titleLabel.text];
is_signin_auto_chkd = [defaults boolForKey:signin_automatic.titleLabel.text];
is_signin_secret_chkd = [defaults boolForKey :signin_secret.titleLabel.text];
This should do the trick for you, extract boolValue from the returned value
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
is_remember_chkd = [[defaults objectForKey:remember_me.titleLabel.text] boolValue];
is_signin_auto_chkd = [[defaults objectForKey:signin_automatic.titleLabel.text] boolValue];
is_signin_secret_chkd = [[defaults objectForKey:signin_secret.titleLabel.text] boolValue];
hope it helps. happy coding :)

find special keys in UserDefaults by "substringToIndex"

I'm grouping my UserDefault keys by specific prefixes.
e.g.
[NSUserDefaults standardUserDefaults] setInteger: 1 forKey: #"prefix1_someText_Key"]
[NSUserDefaults standardUserDefaults] setInteger: 2 forKey: #"prefix2_someText_Key"]
[NSUserDefaults standardUserDefaults] setInteger: 3 forKey: #"prefix4_someText_Key"]
//.....
Now, I'd like to find all the keys, that start with e.g. "prefix", and load them into an array. is there a way for doing that (programmatically)?
You could use the underlying NSDictionary to find the suitable keys:
NSString *myPrefix = #"prefix";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *dict = [defaults dictionaryRepresentation];
NSMutableArray *keysWithPrefix = [NSMutableArray array]
for (NSString *key in dict.keyEnumerator) {
if ([key hasPrefix: myPrefix]) {
[keysWithPrefix addObject: key];
}
}
// now keysWithPrefix contains all matching keys
UPDATE
For debugging reasons you could add a log to see what keys are being dropped:
for (NSString *key in dict.keyEnumerator) {
if ([key hasPrefix: myPrefix]) {
[keysWithPrefix addObject: key];
} else {
NSLog(#"Dropping key %#", key);
}
}

How do I save a UISlider value?

How could I save the value of a UISlider ?I tried this, but it didn't work:
-(IBAction)save {
slider1i = slider1.value;
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setInteger:slider1i forKey:#"slider1key"];
[slider1save synchronize];
}
slider1i is a NSinteger, and slider1 is the UISlider ...
Can you help-me ?
As user 698952 said, UISlider's value property is a float. But i'd instead use setFloat like this:
slider1i = slider1.value;
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setFloat:slider1i forKey:#"slider1key"];
[slider1save synchronize];
slider1.valueis float value so you first need to convert it in NSString or NSNumber inorder to save it ..... that makes it a object
-(IBAction)save {
NSNumber *sliderValue = [NSNumber numberWithFloat:slider1.value];
NSUserDefaults *slider1save = [NSUserDefaults standardUserDefaults];
[slider1save setObject:sliderValue forKey:#"slider1key"];
[slider1save synchronize];
}
thanks

How to store a float in NSUserDefaults

I want to store a float value into NSUserDefaults.
I also need to check that the float value exists..if not I need to assign some value in it.
and retrieve it...for the above I have the below code but it gives me an error.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:#"HANDWRITING_SIZE_SLIDER"] == YES) {
self.sizeSlider.value = 10.0;
} else {
self.sizeSlider.value = [[NSUserDefaults standardUserDefaults] floatForKey:#"HANDWRITING_SIZE_SLIDER"]];
}
Thanks for any help
Use the NSNumber class for this and store it via the setObject:forKey: method so you can check if it exists.
I'd also suggest the usage of constants as keys:
#define HANDWRITING_SIZE_SLIDER #"HSS"
Your code should be along these lines:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:HANDWRITING_SIZE_SLIDER] == nil) {
//doesn't exist in NSUserDefaults, set to default value...
self.sizeSlider.value = 10.0;
} else {
self.sizeSlider.value = [[defaults objectForKey:HANDWRITING_SIZE_SLIDER] floatValue];
}
Somewhere else in your app, you'd set the value in NSUserDefaults like this:
float sizeSliderValue = ...
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:sizeSliderValue] forKey:HANDWRITING_SIZE_SLIDER];
Try this code:
-(IBAction)sliderAction:(id)sender
{
float sliderValue = [sender floatValue];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:sliderValue] forKey:#"keySliderValue"];
NSLog(#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"keySliderValue"]);
}

Freaky UISwitch/UILabel.hidden/UIImageView.hidden Problem!

Sorry for the constant questions, but in my app, you are able to toggle if 3 UILabels and 1 UIImageview's hidden property is YES or NO via UISwitchs on another page (The settings page). The weird part is, one of the UILabels is hidden, even when it is declared that it is not to be hidden. Here is my code on the settings page.
- (IBAction)changeswitch1 {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (artworkswitch.on)
[prefs setInteger:1 forKey:#"AWKey"];
else
[prefs setInteger:0 forKey:#"AWKey"];
}
- (IBAction)changeswitch2 {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (titleswitch.on)
[prefs setInteger:1 forKey:#"TKey"];
else
[prefs setInteger:0 forKey:#"TKey"];
}
- (IBAction)changeswitch3 {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (artistswitch.on)
[prefs setInteger:1 forKey:#"AKey"];
else
[prefs setInteger:0 forKey:#"AKey"];
}
- (IBAction)changeswitch4 {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (volumeswitch.on)
[prefs setInteger:1 forKey:#"VKey"];
else
[prefs setInteger:0 forKey:#"VKey"];
}
All of this is set as the 'value changed' action in IB for the 4 switches.
Here is the code for the main page
if ([prefs integerForKey:#"AWKey"] == 1)
currentArtwork.hidden = NO;
else if ([prefs integerForKey:#"AWKey"] == 0)
currentArtwork.hidden = YES;
if ([prefs integerForKey:#"TKey"] == 1)
currentSong.hidden = NO;
else if ([prefs integerForKey:#"TKey"] == 0)
currentSong.hidden = YES;
if ([prefs integerForKey:#"AKey"] == 1)
currentArtist.hidden = NO;
else if ([prefs integerForKey:#"AKey"] == 0)
currentArtist.hidden = YES;
if ([prefs integerForKey:#"VKey"] == 1)
volumeview.hidden = NO;
else if ([prefs integerForKey:#"VKey"] == 0)
volumeview.hidden = YES;
This is just a guess, since I don't know the res of the code. But maybe it is hidden because another UILabel is positioned infront of it?. You can try:
[self.view bringSubviewToFront:yourLabel];
-Oscar
My solution was to have the label/imageview's hidden property set to NO upon the first launch.