Xet*_*ius 49 iphone cocoa-touch objective-c plist ios
我正在使用以下方法手动创建数据结构:
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];
Run Code Online (Sandbox Code Playgroud)
但是,我认为如果从文件中读取它会更好.所以我创建了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>
Run Code Online (Sandbox Code Playgroud)
我如何读取此文件.我尝试了各种方法,但没有任何工作.我甚至不知道我是否正在寻找合适的文件位置.作为参考,我正在尝试以下方法:
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];
}
Run Code Online (Sandbox Code Playgroud)
要么
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
contentArray = [NSArray arrayWithContentsOfFile:plistPath];
Run Code Online (Sandbox Code Playgroud)
要么
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);
Run Code Online (Sandbox Code Playgroud)
这最终让我完全疯了.请帮忙!
非常感谢你
Ken*_*ner 121
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
contentDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
Run Code Online (Sandbox Code Playgroud)
答案是正确的 - 你确定你的文件在应用程序中吗?您是否已将其添加到项目中,并检查它是否被复制到您的应用程序包中?如果没有,可能是文件未添加到您正在构建的目标中,如果您有多个目标,则很容易犯错误.
| 归档时间: |
|
| 查看次数: |
82060 次 |
| 最近记录: |