I am trying to call a method that prints to a txt file. But then when I call the method again it will just overwrite what was in the text file with my new text. I want to file to just keep adding what is printed.
-(void) print:(NSString *)name
{
NSURL *url = [NSURL fileURLWithPath:@"/here/file.txt"];
NSString *fileContent = [NSString stringWithFormat"@"%@\n", name];
[fileContent writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
When I call this method I am calling it by doing:
[name print: person1.name];
[name print: person2.name];
Everytime I try to print the next name it just replaces the name in the text file. I am unsure how to concantenate and continue adding the names?
Thank you
I have tried this:
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:@"/here/file.txt"];
NSString *textToWrite = @"hello";
[fileHandle seekToEndOfFile];
[fileHandle writeData:[textToWrite dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
Nothing is printing.