2010-10-02 58 views
1

我已經在XCode &中創建了一個pList(myData.plist),但是當我運行該程序時,它沒有被複制到iphone模擬器目錄。因此,我無法讀取它。我已經記錄的文件名,這似乎是指向正確的地方,這裏還有沒有plist文件存在(在Finder中確認)iPhone SDK - plist沒有被複制到模擬器

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 
NSString *pListFileName = [documentsDirectoryPath stringByAppendingPathComponent:@"myData.plist"]; 
NSLog(pListFileName); 
NSFileManager *fileManager = [NSFileManager defaultManager]; 

if ([fileManager fileExistsAtPath: pListFileName]) { 
    self.pList = [[NSMutableDictionary alloc] initWithContentsOfFile:pListFileName]; 
    //code for reading data in plist 
} 

回答

2

兩件事情:

  • 確保它被標記爲包含在目標的包中。

右鍵單擊Xco​​de中項目導航器中的文件,然後選擇「Get Info」。在「目標」標籤中,確保您選中目標的複選框。

這將確保將文件複製到項目的包中。

  • 然後,您必須將其解壓縮到捆綁包中,然後在運行應用程序時將其複製到您的Documents目錄。如果您檢查plist是否在您的文檔目錄中失敗,請從您的軟件包中複製它。

的代碼,但沒有任何必要的錯誤檢查:

if (![fileManager fileExistsAtPath: pListFileName]) { 
    //Copy the file from the app bundle. 
    NSString * defaultPath = [[NSBundle mainBundle] pathForResource:@"myData" ofType:@"plist"]; 
    [fileManager copyItemAtPath:defaultPath toPath:pListFileName error:NULL] 
} 

self.pList = [[NSMutableDictionary alloc] initWithContentsOfFile:pListFileName]; 

同樣,我刪除的任何錯誤檢查,但你應該包括在你的產品代碼。

+0

完美,謝謝!它在捆綁中,但我沒有複製它。 – Gary 2010-10-02 23:39:49

+0

謝謝,忘了把它也包含進去。 – Max 2013-08-10 20:33:28