user843337
user843337

Reputation:

Reading NSUserDefault bool

I have a problem where I'm trying to register a bool as an NSUserDefault. I then want to be able to read this bool value later on. The issue is that when I read the value its not picking it up as a YES.

Here is the code I use to register the value:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSMutableDictionary *appDefaults = [NSMutableDictionary dictionary];
[appDefaults setValue:@"YES"    forKey:kShowHelp];

[defaults registerDefaults:appDefaults];
[defaults synchronize];

I then read the bool using the following:

bool showHelp = [[NSUserDefaults standardUserDefaults] boolForKey:kShowHelp];

Upvotes: 0

Views: 1002

Answers (2)

Macmade
Macmade

Reputation: 53960

Why aren't you using setBool:forKey?

[ appDefaults setBool: YES forKey: kShowHelp ];

EDIT

Ok, since you have a dictionary, you should use a NSNumber to represent a boolean values.

Example

NSUserDefaults * defaults = [ NSUserDefaults standardUserDefaults ];

NSMutableDictionary * appDefaults = [ NSMutableDictionary dictionary ];
[ appDefaults setObject: [ NSNumber numberWithBool: YES ] forKey: kShowHelp ];

[ defaults registerDefaults: appDefaults ];
[ defaults synchronize ];

EDIT 2

Do you know you can also create a plist in your application's bundle, and register its values as defaults? Much simpler IMHO.

Upvotes: 1

Jason Coco
Jason Coco

Reputation: 78353

Don't use setValue:forKey: use setObject:forKey:

[appDefaults setObject:[NSNumber numberWithBool:YES] forKey:kShowHelp];

Upvotes: 4

Related Questions