I’m trying to mirror what I write to console to a text file. Here’s the code I’ve written, but it isn’t creating the txt file. Any help is greatly appreciated:
-(NSString *) display
{
NSLog (@" \n ");
NSLog (@"|========== Contents of: %@ =============|", listName);
NSLog (@"|__________________________________________________|");
NSLog (@"| | Song Title | | | Artist | |");
NSLog (@"| ============== | ========== |");
NSLog (@"|~~~~~~~~~~~~~~~~~~~~~~|~~~~~~~~~~~~~~~~~~~~~~~~~~~|");
for ( SongRecord *theRecord in list )
NSLog (@"| %-20s| %-24s |", [theRecord.songname UTF8String],
[theRecord.artist UTF8String]);
NSLog (@"====================================================");
return listing;
}
-(void) saveToFile
{
NSString* fileName = @"Mysongs.txt";
NSString* fullPath = [path stringByAppendingPathComponent:fileName];
[[self display] writeToFile:fullPath atomically:YES encoding:NSUnicodeStringEncoding error:nil];
NSLog (@"The file has been written");
}
Modify
-displayso that it builds a string that you can actually return for use in-saveToFile. At the same time, you can send it to console, as you wish: