How do I save two strings and a BOOL to a plist?

Discussion in 'iOS Programming' started by Dippo, Aug 14, 2008.

  1. Dippo macrumors 65816

    Dippo

    Joined:
    Sep 27, 2003
    Location:
    Charlotte, NC
    #1
    How do I save two strings and a BOOL to a plist?

    Or at least what example shows this?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    If we assume that the strings and bools are the variables I declare:

    Code:
    
    NSString *stringToSave1;
    NSString *stringToSave2;
    BOOL boolToSave;
    
    // Do whatever gets the above setup.
    
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    [defs setObject:stringToSave1 forKey:@"string 1"];
    [defs setObject:stringToSave2 forKey:@"string 2"];
    [defs setBool:boolToSave forKey:@"bool"]
    
    You can recover them from defaults by

    Code:
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    stringToSave1  = (NSString *) [defs objectForKey:@"string 1"];
    stringToSave2  = (NSString *) [defs objectForKey:@"string 2"];
    boolToSave = [defs boolForKey:@"bool"];
    
    I would not that this is all amazingly clear in the NSUserDefaults documentation. Please check the documentation before asking basic questions. The ability to read and understand the documentation is a key programming skill.
     

Share This Page