如何将.plist文件读取数据结构到nsarray
-
09-09-2019 - |
题
我正在使用以下内容手动创建数据结构:
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];
但是,我认为如果从文件中读取它会更好。因此,我创建了具有以下结构的文件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];
该答案是正确的 - 您确定您的文件在应用中吗?您是否将其添加到您的项目中,并检查是否已复制到您的应用程序捆绑包中?如果没有,则可能不是将文件添加到您要构建的目标中,这是一个简单的错误,尤其是如果您有多个目标。
其他提示
如果是 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中的“资源”文件夹中。
为了完整,肯德尔和本特福德是完全正确的。但是,在我的示例中,contentarray是一个属性,到了方法结束时,它就不在范围了,因为 ArrayWithContentsOffile 创建一个自动释放的对象。
为了使这项工作正确,我需要做3件事:
将文件放入资源文件夹中
正确命名文件(是联赛。
使用 [NSARRAY ALLOC] initwithContentsOffile:plistPath)];
第三部分会创建一个分配的nsarray,当您退出此功能的范围时不会发行...当然,这需要在DealLoc函数中发布。
只是要添加。我遇到了同样的问题,建议的解决方案帮助我解决了问题,但是我不确定我是否实际使用了确切的解决方案。在我的情况下,问题在于,.plist文件已添加到另一个目标中(片刻之前添加了一个新目标)。因此,解决方案是.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"]);