Tutorial :NSString read/write to file


Im just having trouble reading and writing NSString to a dat file on the iphone.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentsDirectory = [paths objectAtIndex:0];    NSString *saveFile = [documentsDirectory stringByAppendingPathComponent:@"Profiles.dat"];    Boolean saveFileExists = [[NSFileManager defaultManager] fileExistsAtPath:saveFile];      // NSString *saveFile = [documentsDirectory stringByAppendingPathComponent:@"Profiles.dat"];        NSMutableData *gameData;    NSKeyedUnarchiver *decoder;      NSKeyedArchiver *encoder;      //decoder  = [[NSKeyedUnarchiver alloc] initForWritingWithMutableData:gameData];      myGameState *gameState = [myGameState sharedMySingleton];        if(saveFileExists) {     gameData = [NSData dataWithContentsOfFile:saveFile];     decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];       gameState.Profile1 = [decoder decodeObjectForKey:@"Name1"];     gameState.Profile2 = [decoder decodeObjectForKey:@"Name2"];     gameState.Profile3 = [decoder decodeObjectForKey:@"Name3"];     gameState.Profile4 = [decoder decodeObjectForKey:@"Name4"];     gameState.Profile5 = [decoder decodeObjectForKey:@"Name5"];       gameState.curProfile = [decoder decodeObjectForKey:@"curProfile"];     [decoder release];        }    else {     gameData = [NSMutableData data];     encoder  = [[NSKeyedArchiver alloc] initForWritingWithMutableData:gameData];     gameState.Profile1 = @"Default1";     gameState.Profile2 = @"Default2";     gameState.Profile3 = @"Default3";     gameState.Profile4 = @"Default4";     gameState.Profile5 = @"Default5";       [encoder encodeObject:@"Default1" forKey:@"Name1"];     [encoder encodeObject:@"Default2" forKey:@"Name2"];     [encoder encodeObject:@"Default3" forKey:@"Name3"];     [encoder encodeObject:@"Default4" forKey:@"Name4"];     [encoder encodeObject:@"Default5" forKey:@"Name5"];       gameState.curProfile = gameState.Profile1;     [encoder encodeObject:gameState.curProfile forKey:@"curProfile"];       [encoder finishEncoding];     //[gameData writeToFile:<#(NSString *)path#> atomically:<#(BOOL)useAuxiliaryFile#>     [gameData writeToFile:saveFile atomically:YES ];     //[gameData writeToFile:saveFile atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];       [encoder release];      }  

gameState.curProfile etc are all NSString pointers. Now im very new to Objective C and just cant seem to get this working correctly. I have searched around forums and cant seem to find the correct way to do this. Literally all i want is to read a write a NSString to a dat file but it aint working as is. I have managed to write ints and bools with no problems just cant get past the text problem on my own. any help is appreciated thanks g


Your code works without any problem here. I guess you have problems in other parts of codes.

By the way, if you only need to save a few entries, you don't have to do all these works yourself; just use NSUserDefaults. It automatically prepares the file to save the data, encodes the data, and decodes the data on the next launch. See this Apple document. It can be used as

 [[NSUserDefaults standardUserDefaults] setString:@"boo" forKey:@"key!"];  

and later the data can be read as

 NSString* s=[[NSUserDefaults standardUserDefaults] stringForKey:@"key!"];  

It's as easy as this! You might want to call

[[NSUserDefaults standardUserDefaults] synchronize];  

to force the saving of the data to the file; usually this isn't necessary (The system does this automatically when quit, etc.)

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »