如何从.plist文件读取数据结构到NSArray中

我正在使用以下方法手动创build数据结构:

NSDictionary* league1 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Barclays Premier League", @"name", @"Premier League", @"shortname", @"101", @"id", nil]; NSDictionary* league2 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Coca-Cola Championship", @"name", @"Championship", @"shortname", @"102", @"id", nil]; NSDictionary* league3 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Scottish Premier League", @"name", @"SPL", @"shortname", @"201", @"id", nil]; NSDictionary* league4 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Champions League", @"name", @"UCL", @"shortname", @"501", @"id", nil]; contentArray = [[NSArray alloc] initWithObjects: [[NSDictionary alloc] initWithObjectsAndKeys: @"English", @"category", [[NSArray alloc] initWithObjects: league1, league2, nil], @"leagues", nil], [[NSDictionary alloc] initWithObjectsAndKeys: @"Scottish", @"category", [[NSArray alloc] initWithObjects: league3, nil], @"leagues", nil], [[NSDictionary alloc] initWithObjectsAndKeys: @"Tournaments", @"category", [[NSArray alloc] initWithObjects: league4, nil], @"leagues", nil], nil]; [league1 release]; [league2 release]; [league3 release]; [league4 release]; 

不过,我认为如果从文件中读取它会更好。 所以我创build了具有以下结构的文件leagues.plist:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <dict> <key>category</key> <string>English</string> <key>leagues</key> <array> <dict> <key>name</key> <string>Barclays Premier League</string> <key>shortname</key> <string>Premier League</string> <key>id</key> <string>101</string> </dict> <dict> <key>name</key> <string>Coca-Cola Championship</string> <key>shortname</key> <string>Championship</string> <key>id</key> <string>102</string> </dict> </array> </dict> <dict> <key>category</key> <string>Scottish</string> <key>leagues</key> <array> <dict> <key>name</key> <string>Scottish Premier League</string> <key>shortname</key> <string>SPL</string> <key>id</key> <string>201</string> </dict> </array> </dict> <dict> <key>category</key> <string>Tournaments</string> <key>leagues</key> <array> <dict> <key>name</key> <string>Champions League</string> <key>shortname</key> <string>UCL</string> <key>id</key> <string>501</string> </dict> </array> </dict> </array> </plist> 

我如何阅读这个文件。我尝试了各种方法,但没有任何工作。 我甚至不知道我是否在正确的地方寻找文件。 作为参考,我正在尝试以下方法:

 NSString* errorDesc = nil; NSPropertyListFormat format; NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"]; NSData* plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; contentArray = (NSArray*)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; if (!contentArray) { NSLog(errorDesc); [errorDesc release]; } 

要么

 NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"]; contentArray = [NSArray arrayWithContentsOfFile:plistPath]; 

要么

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; NSString *fooPath = [documentsPath stringByAppendingPathComponent:@"leagues.plist"]; NSLog(fooPath); contentArray = [NSArray arrayWithContentsOfFile:fooPath]; NSLog(@"%@",contentArray); 

这终于使我完全疯了。 请帮助!

非常感谢你

 NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"]; contentDict = [NSDictionary dictionaryWithContentsOfFile:plistPath]; 

这个答案是正确的 – 你确定你的文件在应用程序? 你把它添加到你的项目中,并检查它是否被复制到你的应用程序包? 如果没有,可能是文件没有被添加到你正在构build的目标中,特别是如果你有多个目标,很容易犯的错误。

如果plist位于项目的资源文件夹中,请使用此代码。

  NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"]; contentArray = [NSArray arrayWithContentsOfFile:sourcePath]; 

如果plist位于应用程序的文档目录中,请使用以下命令:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; NSString *plistName = @"league"; NSString *finalPath = [basePath stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.plist", plistName]]; contentArray = [NSArray arrayWithContentsOfFile:finalPath]; 

我有这个问题,但它不工作,因为我是从pList的结果放到一个数组,它应该是一个字典,即

 NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"VehicleDetailItems" ofType:@"plist"]; NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; 

肯德尔是正确的。

根据我的经验,您需要将您的文件添加到xcode中的“Resources”文件夹中。

为了完整,Kendall和bentford是完全正确的。 不过,在我的示例中,contentArray是一个属性,在方法结束时,它将超出范围,因为arrayWithContentsOfFile创build一个自动释放的对象。

为了使这个工作正确,我需要做3件事情:

  1. 将该文件放在资源文件夹中

  2. 正确命名文件(是leagues.plist而不是league.plist)

  3. 使用[[NSArray alloc] initWithContentsOfFile:plistPath)]读取文件;

第三部分创build一个分配的NSArray,当你退出这个函数的范围时不会释放…当然,这需要在dealloc函数中释放。

只需添加。 我有同样的问题,并build议的解决scheme帮助我解决了这个问题,但是我不确定我是否真的使用了确切的解决scheme。 在我的情况下,问题是.plist文件被添加到不同的目标(之前添加了一个新的目标)。 因此,解决scheme是.plist>获取信息>目标,并确保将其添加到正确的目标,以便在安装时将其复制到设备。 如果我已经知道了这一点,我会省下很多时间。 希望这也有帮助。 问候!

如果该文件未被添加到资源文件夹中,并且只放置在文档目录中。 你可以这样做

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.plist"]; NSMutableArray *arrContentsplist = [[NSMutableArray alloc] initWithContentsOfFile:path]; 

此代码正在工作

 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plist-name" ofType:@"plist"]; NSDictionary *Dictionary= [[NSDictionary alloc]initWithContentsOfFile:plistPath]; NSLog(@" current version CFBundleVersion = %@",[Dictionary valueForKey:@"CFBundleVersion"]);