简体   繁体   中英

read/write from a .txt file iOS 7 objc

I have this code:

  NSString *studentList = _textIn.text;
NSString *path = [[self applicationDocumentsDirectory].path
                  stringByAppendingPathComponent:@"ClassOne.txt"];
[studentList writeToFile:path atomically:YES
               encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", studentList);
NSLog(@"students 1 saved");

I try to write to a text file on the phone system. When I try to read from that file with:

NSError *error;
NSString *strFileContent = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]
                                                               pathForResource: @"classOne" ofType: @"txt"] encoding:NSUTF8StringEncoding error:&error];
_textIn.text = strFileContent;

The textfield comes up blank. The NSLog prints what was in the text field but when I try to load it up later the textfield is empty. how do I get the textfield to display what is in the file.

The writing part is alright. You are trying to read the file from your application main bundle instead of your original file path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM