2008-12-01 63 views
1

我有一個plist文件,我想解析它並將其內容複製到NSArray中,並且我正在使用的代碼是。解析.plist文件中的問題

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 

NSString *documentsPath = [paths objectAtIndex:0]; 

NSString *fooPath = [documentsPath stringByAppendingPathComponent:@"myfirstplist.plist"]; 
NSLog(fooPath); 
self.myArray = [[NSArray arrayWithContentsOfFile:fooPath] retain]; 
NSLog(@"%@",myArray); 

現在問題很奇怪,有時候當我打印myArray內容時,它會打印文件數據,有時候它不會。

即使我使用URL作爲路徑,我也面臨同樣的問題。

self.myArray = [[NSArray arrayWithContentsOfURL:URlPath] retain]; 

會是什麼原因?

在此先感謝。

+0

不打印數組內容時,會發生什麼?是否有錯誤,或日誌是否顯示(空)? – 2008-12-01 06:52:19

回答

4

根據您最初生成.plist的方式,如果嘗試以陣列形式讀取它,可能會遇到問題。讀取plist中最安全的方式是使用NSPropertyListSerialization類:Apple Doc.

0

這是非常愚蠢的錯誤,
我宣佈「myArray的」屬性「保留和非原子」和解析工作期間,我再次將其保留,

self.myArray = [[NSArray arrayWithContentsOfURL:URlPath] retain]; 

意味着我保留它但從未釋放它。這就是爲什麼那個奇怪的問題在那裏。

乾杯。

1

要獲取路徑中使用

NSString *plistPath = [bundle pathForResource: @"file-name" ofType:@"plist"]; 

,然後用它

NSArray *phrase2 = [NSArray arrayWithContentsOfFile: plistPath]; 
NSLog (@"%@", phrase2); 
1
NSBundle *bundle = [NSBundle mainBundle]; 

NSString *plistPath = [bundle pathForResource: @"file-name" ofType:@"plist"]; 

NSArray *phrase2 = [NSArray arrayWithContentsOfFile: plistPath]; 

NSLog (@"%@", phrase2);