2010-03-22 94 views
4

我在xcode的資源組中有一個plist文件。我試圖在應用程序啓動時將其複製到我的文檔目錄中。我使用下面的代碼(從源碼教程拍攝):Plist不會複製到文檔目錄

BOOL success; 
NSError *error; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingString:@"ActiveFeedsPlist.plist"]; 

success = [fileManager fileExistsAtPath:filePath]; 
if (success) return; 

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"ActiveFeedsPlist.plist"]; 
success = [fileManager copyItemAtPath:path toPath:filePath error:&error]; 

if (!success) { 
    NSAssert1(0, @"Failed to copy Plist. Error %@", [error localizedDescription]); 
} 

我給出的錯誤「***終止應用程序由於未捕獲的異常‘NSInternalInconsistencyException’,原因:「無法複製的plist錯誤操作無法完成,但在控制檯中沒有這樣的文件或目錄''。

任何想法是什麼錯?

感謝

回答

10

你缺少一個文件分隔符:

... stringByAppendingString:@"/ActiveFeedsPlist.plist"]; 

,或者更好,用途:

... stringByAppendingPathComponent:@"ActiveFeedsPlist.plist"]; 
+1

或stringByAppendingPathComponent:@ 「ActiveFeedsPlist.plist」 – dstnbrkr 2010-03-22 02:30:58

+1

在這種情況下,最好使用'-stringByAppendingPathComponent:'。 (哦,打了一分鐘!) – Wevah 2010-03-22 02:32:03

+1

@dbarker,偉大的思想!大聲笑我在編輯這篇文章的同時發佈了評論。 – nicerobot 2010-03-22 02:32:41