如何使用plist保存和检索UITextView中的文本

时间:2011-04-05 08:28:54

标签: iphone

您好如何保存和检索UITextView中的文本?如果有人知道请帮助我。使用plist给我一些示例代码。 谢谢。

1 个答案:

答案 0 :(得分:1)

查看stringWithContentsOfFile:encoding:error:writeToFile:atomically:encoding:error:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myfile.plist"];
NSError *error = nil;

// read from plist:
NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (!text) {
    NSLog(@"Error reading - %@", error);
}
textView.text = text;

// save to plist:
NSString *text = textView.text
if (![text writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
    NSLog(@"Error saving - %@", error);
}