Hi all 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