你的道路是错误的。如果你的plist在你的应用程序资源中,那么你必须使用NSBundle
来获得它的绝对路径。
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
NSMutableDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path]
mutableCopy];
如果它在你的应用程序的文件夹,你可以这样做:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *path = [basePath stringByAppendingPathComponent:@"data.plist"];
NSMutableDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path] mutableCopy];
注意,从plist中加载的字典或阵列始终是一成不变的。你必须创建一个可变副本。
我的plist在_〜/图书馆/应用程序支持/ iPhone模拟器/ iOS /应用程序/ MY-APP/Documents_,但此代码不起作用,有什么想法? – user1269586
请参阅[this](http://stackoverflow.com/a/6907432/343340)了解如何获取文档文件夹路径。并看到我更新的答案。 – DrummerB
的NSLog等于 “(空)” 再次... 但我的代码看起来不错: ' - (无效)viewDidAppear:(BOOL)动画{ 的NSArray *路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,SUserDomainMask,YES); NSString * basePath =([paths count]> 0)? [paths objectAtIndex:0]:nil; NSString * path = [basePath stringByAppendingPathComponent:@“data-article-itiap.plist”]; NSMutableDictionary * dict = [[NSDictionary dictionaryWithContentsOfFile:path] mutableCopy]; NSLog(@“%@”,[dict objectForKey:@“id”]); }' – user1269586