2011-04-06 33 views
0

這是代碼,我如何保存文本如何檢索短信

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0]; 
    NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"myFile.plist"]; 
    NSString *savedString = _textView.text; 
    //NSString *saveString=_textView2.text; 
    [savedString writeToFile:documentTXTPath atomically:YES ]; 

請告訴我如何找回它。請幫幫我。以及如何在我的項目中製作plist。 謝謝。

回答

0

快速的方法是:

NSString *myText = [NSString stringWithContentsOfFile:filePath 
          encoding: NSUTF8StringEncoding 
          error: NULL]; 

不要忘記做你的錯誤處理。

請注意,這將阻塞主線程,直到整個文本文件被加載。如果您正在處理大型文件,則可能需要使用mmap and munmap

作爲一個側面說明,如果你曾經想在網絡上加載一個文本文件,從未使用NSString stringWithContentsOfURL(或任何的一般* WithContentsOfURL方法)。該方法將阻塞主線程,直到文件被下載。在這種情況下,請改用NSURLConnectionASIHTTPRequest

+0

非常感謝。它有助於回顧 – Priya 2011-04-06 09:11:50

0

我想,你可能會想知道,如何處理與應用的plist ...

這裏是SO後襬脫plist中的數據和寫的plist ...

How to read a plist data and get int from it?

How to write the data to the plist

這裏是從plist中讀取字典中的數據的代碼...

NSString *path = [[NSBundle mainBundle] bundlePath]; 
NSString *myplistPath = [path stringbyAppendingPathComponent:@"myplist.plist"]; 

NSDictionary *myDictionary = [[NSDictionary alloc] initWithContentsOfFile:myplist];