2011-02-16 160 views
2

我想寫一個數組到一個plist,當我在文檔文件夾中手動創建plist時,它可以正常工作。創建一個plist文件

但是當我檢查的plist存在,如果不從主束沒有發生plist中創建.....

我做到了作爲Property List Programming Guide它被寫入。

NSString *plistRootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *plistPath = [plistRootPath stringByAppendingPathComponent:@"list.plist"]; 

if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { 

    plistPath = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"]; 

} 

我錯過了什麼?

編輯:

我一個單獨的應用程序tryed的功能和它的工作。 所以我再次tryed它:

NString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"list.plist"]; 

self.ListArray = [NSMutableArray arrayWithContentsOfFile:plistPath]; 
NSLog(@"preSave: %@", [ListArray count]); 
NSLog(@"%@", plistPath); 

和日誌說:

2011-02-16 16:54:56.832 ListApp[3496:207] .../Library/Application Support/iPhone Simulator/4.0.2/Applications/267F55E4-A46A-42E0-9E1C-EAF26846F75F/Documents/list.plist 

但在迪爾沒有文件!

+0

缺碼,你實際上創建陣列?像數組= = [NSArray arrayWithContentsOfFile:plistPath]? – Vladimir 2011-02-16 14:15:42

回答

0

我tryed上的另一臺Mac上的代碼,有它的工作原理這是同一個項目。

任何人都可以解釋爲什麼plist是在一個mac上創建的,而不是在另一個上創建的?

在iPhone上也沒有plist寫的?

我在這兩種情況下想這是導致我寫的plist數組爲空:

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"list.plist"]; 
NSMutableArray *currentArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; 
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
      [selectet valueForKey:NAME], @"name", 
      nil]; 

NSLog(@" DIC. : %@", dictionary); 
[currentArray addObject:[dictionary copy]]; 
NSLog(@" %@", currentArray); 
[currentArray writeToFile:plistPath atomically:YES]; 
[currentArray release]; 

詞典中的條目,但currentArray是空的.... 而且還是它的工作原理上的一個MAC 。但不在另一方面和iPhone上。

編輯:

出於某種原因,一切又恢復了。

爲了確保我從文檔文件夾中刪除了plist並清除了所有目標。

而現在的問題,因爲之前我當我寫字典中只能寫的plist但當時我只能保存最後一個。 閱讀plist也無濟於事。

誰能告訴我爲什麼發生這種情況,我該如何解決它?

編輯2:

我發現這個問題:因爲當我寫的陣列字典到plist.When X-代碼不會創建plist中我寫的字典plist中那麼X -Code創建的plist,但使用一本字典的根,所以我不能在陣列寫它,我也不能以陣列根數組中的閱讀.....

我的資源文件夾中創建的plist並複製它不存在文檔文件夾,現在它的工作.....

是不是很神奇容易的事情怎麼能這樣給你這樣一個頭痛

2
NSString *DocPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

filePath=[DocPath stringByAppendingPathComponent:@"AlarmClock.plist"]; 

if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
{ 
    NSString *path=[[NSBundle mainBundle] pathForResource:@"AlarmClock" ofType:@"plist"]; 
    NSLog(@"file path: %@",filePath); 
    NSDictionary *info=[NSDictionary dictionaryWithContentsOfFile:path]; 
    [info writeToFile:filePath atomically:YES]; 
} 

// * * *嘗試......