2008-12-01 64 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);