[英]read/write from a .txt file iOS 7 objc
我有以下代碼:
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");
我嘗試在電話系統上寫入文本文件。 當我嘗試從該文件讀取時:
NSError *error;
NSString *strFileContent = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]
pathForResource: @"classOne" ofType: @"txt"] encoding:NSUTF8StringEncoding error:&error];
_textIn.text = strFileContent;
文本字段為空白。 NSLog打印出文本字段中的內容,但是當我稍后嘗試加載它時,文本字段為空。 如何獲取文本字段以顯示文件中的內容。
寫作部分還不錯。 您正在嘗試從應用程序主捆綁包中讀取文件,而不是原始文件路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.