2010-02-04 57 views
1

這一定是容易的,但我想提出一個文件,其中在啓動時讀取該文件夾。我有關於如何閱讀並確認其在正確的目錄中查找的代碼。然而,我保存在xcode的Resources文件夾中的RootList.txt文件存儲在Root.app文件夾下,並且Documents文件夾爲空。因此,當我啓動應用程序時,它沒有找到這個文件。iphone存儲和讀取,然後從文檔文件夾

有沒有一種方法可以確保文件在啓動時建立到文檔目錄中(我在模擬器中運行這個文件)。

替代方案是一個plist,它工作得很好,但我只是好奇。

回答

7

在這種情況下,我按照這個辦法:

首先保存RootList.txt在xCode.You資源文件夾中有沒有在你的文檔文件夾,但。

在你applicationDidLaunch呼叫的開始,做:

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *path = [docsDirectory stringByAppendingPathComponent:@"RootList.txt"]; 
if(![fileManager fileExistsAtPath:path]) 
{ 
NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/RootList.txt"]]; 
[data writeToFile:path atomically:YES]; 
} 

現在,你的文件是在文檔文件夾中在啓動時。

+0

謝謝ahmet,這是有道理的 – user238712

相關問題