tags:

views:

135

answers:

1

Hello everyone I use the codes below to read and write a bool value from my application:

-(void)SaveAppSetting;
{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];

    bool b=true;
    [defaults setBool:b forKey:@"AnyKey"];
    [defaults synchronize];
}
-(void)LoadAppSetting;
{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    bool b=[defaults boolForKey: @"AnyKey"] ;


}

I found that "LoadAppSetting" worked well, it could get the correct value of item with key "AnyKey". The function "SaveAppSetting" looked like no function, it reported no error, but it can not change value of the item with key "AnyKey".

"AnyKey" setting in Setting bundle is

item               Dictionary
Type               String       PSToggleSwitchSpecifier
Title              String       AnyKey's Title
Key                String       AnyKey
DefaultValue       Boolean      Checked

Is there any person met the same problem? Thanks interdev

A: 

Have you tried using the BOOL type. I am not 100% sure they are exactly the same. BOOL is a char type and I think bool is int.

-(void)SaveAppSetting;
{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];

    BOOL  b = YES;
    [defaults setBool:b forKey:@"AnyKey"];
    [defaults synchronize];
}
-(void)LoadAppSetting;
{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    BOOL b=[defaults boolForKey: @"AnyKey"] ;
}

Really should not make a difference as type conversion should happen. For what it is worth, I use the following to get a BOOL out:

+ (void) setXEntryHighlighted:(BOOL) newValue
{
    [[NSUserDefaults standardUserDefaults] setBool:newValue forKey:DEFAULTS_XENTRY_HIGHLIGHTED];
}
+ (BOOL) xEntryHighlighted
{
    BOOL   rValue = [[NSUserDefaults standardUserDefaults] boolForKey:DEFAULTS_XENTRY_HIGHLIGHTED];
    return (rValue);
}

I Sync at a later point to avoid frequent writes.

Steven Noyes