Help with NSUserDefaults

Discussion in 'iOS Programming' started by Jeremy1026, Sep 24, 2008.

  1. macrumors 68000

    Joined:
    Nov 3, 2007
    #1
    So, I figured out how to pull NSUserDefaults using a settings bundle. But I am trying to do it without a bundle. I just want to save a variable called "theLevel" at a defined point. Then call that variable at the launch (or awakeFromNib) of the application. Can anyone help me with this?
     
  2. thread starter macrumors 68000

    Joined:
    Nov 3, 2007
    #2
    Alright, so I am able to store the value (converted from an int to a string.) It gets saved in my applications preference folder in "com.yourcompany.ChainAttraction.plist" But I am having problems reading that value?

    Here is the code I am trying to use to save and load.

    Code:
    -(void)saveToUserDefaults:(NSString*)levelString
    {
    	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    
    	if (standardUserDefaults) {
    		[standardUserDefaults setObject:levelString forKey:@"highlevel"];
    		[standardUserDefaults synchronize];
    	}
    }
    
    -(NSString*)retrieveFromUserDefaults
    {
    	NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    	NSString *val = nil;
    	
    	if (standardUserDefaults) 
    		val = [standardUserDefaults objectForKey:@"highlevel"];
    	
    	return val;
    }
    I modified the code found at http://www.cocoadev.com/index.pl?NSUserDefaults
     
  3. thread starter macrumors 68000

    Joined:
    Nov 3, 2007
    #3
    GOT IT GOT IT! I don't know why that code was going through the trouble of the if statement...anywho.

    Simlpy use
    Code:
    NSString *val = [standardUserDefaults objectForKey:@"theKey"];
    to pull the value.
     

Share This Page