Writing data to the disk?

Discussion in 'iOS Programming' started by MythicFrost, Dec 20, 2009.

  1. MythicFrost macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #1
    Alright, I want to write the username that is input into the text box onto the disk so it's saved.
    I've found this function at the iPhone Dev Center
    Code:
    - (BOOL)writeApplicationPlist:(id)plist toFile:(NSString *)fileName {
        NSString *error;
        NSData *pData = [NSPropertyListSerialization dataFromPropertyList:plist format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error];
        if (!pData) {
            NSLog(@"%@", error);
            return NO;
        }
        return ([self writeApplicationData:pData toFile:(NSString *)fileName]);
    }
    But I am unsure what to pass as the first parameter? this converts the plist to a data form to be saved, but what can I do to make my plist programatically?

    I've already got the path stored in the variable sSettingsPath where I will write the data to.

    I've also found this function, which would be better?
    I want to store the username, and then other ints containing details about the character the user will be using in the game.

    Other function:
    Code:
    - (BOOL)writeApplicationData:(NSData *)data toFile:(NSString *)fileName {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        if (!documentsDirectory) {
            NSLog(@"Documents directory not found!");
            return NO;
        }
        NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
        return ([data writeToFile:appFile atomically:YES]);
    }
    Any idea's? Thanks.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    That's way, way overcomplicated for writing a single username which is basically a user preference. Use NSUserDefaults

    Set:
    Code:
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:myUserNameNSString forKey:@"username];
    [def synchronize]
    
    Retrieve:
    Code:
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *myUserNameNSString = [def stringForKey:@"username"];
    
     

Share This Page