6

As a practice, I am trying to write an app similar to the built-in notes app.
But I cannot figure out how to save the file and display it in a UITableView.
Right now, I have a UITextView that the user can type in. I also have a save button.
When the user taps the save button, I want to save it, and later have it displayed in a table view.
I am very lost so if you know of any relevant tutorials etc. it would be greatly appreciated.

mipe34
  • 5,596
  • 3
  • 26
  • 38
tallen11
  • 1,387
  • 2
  • 17
  • 33
  • 1
    An answer that you probably don't want to hear is "Don't use text files". You could have a look at some built in serialization strategies (plists). – hanno Feb 11 '11 at 00:18
  • 1
    @hanno - I would also add that even better than plists, use Core Data. It's fast, efficient and more importantly, *scalable*. – Wayne Hartman Feb 11 '11 at 00:24

2 Answers2

23

As noted by the commenters in the real world, you're definitely going to want to look at Core Data or some other data persistence strategy. If you're dead set on pursuing this as a learning experience, something like this should solve your problem:

- (void)writeStringToFile:(NSString*)aString {

    // Build the path, and create if needed.
    NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* fileName = @"myTextFile.txt";
    NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];

    if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
        [[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];
    }

    // The main act...
    [[aString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO];
}

- (NSString*)readStringFromFile {

   // Build the path...
   NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
   NSString* fileName = @"myTextFile.txt";
   NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];

   // The main act...
   return [[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileAtPath] encoding:NSUTF8StringEncoding] autorelease];
}
Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
Matt Wilding
  • 20,115
  • 3
  • 67
  • 95
  • Is it better than using [NSString writeToFile](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html) ? – Pierre de LESPINAY Feb 14 '13 at 16:00
  • hello. I got error and crash at this line :[[aString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO]; – kemdo Dec 22 '17 at 15:14
1

The easiest way to save text is using NSUserDefaults.

[[NSUserDefaults standardUserDefaults] setObject:theText forKey:@"SavedTextKey"];

or, if you want to have the user name each "file" or be able to have multiple files

NSMutableDictionary *saveTextDict = [[[[NSUserDefaults standardUserDefaults] objectForKey:@"SavedTextKey"] mutableCopy] autorelease];
if (saveTextDict == nil) {
    saveTextDict = [NSMutableDictionary dictionary];
}

[saveTextDict setObject:theText forKey:fileName];
[[NSUserDefaults standardUserDefaults] setObject:saveTextDict forKey:@SavedTextKey"];
Ray Lillywhite
  • 746
  • 5
  • 12
  • If all that's needed is a quick-and-dirty string saving then absolutely go for the plist approach. But if you're going to scale up at all, try and resist the temptation offered by its ease of use. – Matt Wilding Feb 11 '11 at 01:06