ios7 - NSUserDefaults does not pick up setting -


i quite new coding in objective c , using settings bundle here coded.

    // set application defaults     nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];     nsdictionary *appdefaults = [nsdictionary dictionarywithobject:@"yes"                                                         forkey:@"gamesave"];     [defaults registerdefaults:appdefaults];     [defaults synchronize];        // user preference     gamesave = [defaults boolforkey:@"savegame"];     nslog(@"save game = %@", gamesave ? @"yes" : @"no"); 

this settings bundle:

<?xml version="1.0" encoding="utf-8"?> <!doctype plist public "-//apple//dtd plist 1.0//en"       "http://www.apple.com/dtds/propertylist-1.0.dtd"> <plist version="1.0"> <dict> <key>preferencespecifiers</key> <array>     <dict>         <key>title</key>         <string>xxxxxxxxxxxxx</string>         <key>type</key>         <string>psgroupspecifier</string>         <key>footertext</key>         <string>www.xxxxxxxxxxxx.com</string>     </dict>     <dict>         <key>defaultvalue</key>         <true/>         <key>key</key>         <string>gamesave</string>         <key>title</key>         <string>save game</string>         <key>type</key>         <string>pstoggleswitchspecifier</string>     </dict> </array> <key>stringstable</key> <string>root</string> </dict> </plist> 

when tested gamesave value no or false. can point me solution? thanks.

@"yes" nsstring, not nsnumber containing boolean.

you want @yes instead:

nsdictionary *appdefaults = [nsdictionary dictionarywithobject:@yes                                                     forkey:@"gamesave"]; 

or same, shorter:

nsdictionary *appdefaults = @{@"gamesave": @yes}; 

edit: if do... this?

// set application defaults -- if not written yet nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; if ([defaults objectforkey:@"gamesave"] == nil) {     [defaults setbool:yes forkey:@"gamesave"];     [defaults synchronize]; }  // user preference gamesave = [defaults boolforkey:@"gamesave"]; nslog(@"save game = %@", gamesave ? @"yes" : @"no"); 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -