在plist文件读取的值显示在UITableView中时是空值
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>Title</key>
<string>Mr</string>
<key>First Name</key>
<string>xxxxxx</string>
<key>Last Name</key>
<string>yyyyyyy</string>
<key>Designation</key>
<string>Senior Engineer</string>
</dict>
</array>
</plist>
代码:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *valueArray = [dict allKeys];
/*
Printing description of path:
/Users/rock/Library/Application Support/iPhone Simulator/6.0/Applications/2567D2B3-56D4-4464-9EFC-CAB923ABE955/PlistTableView.app/Data.plist
Printing description of dict:
<nil>
Printing description of valueArray:
<nil>
*/
现在的结果是dictionary和数组都是空值,不知道应该怎么办。请帮忙谢谢。
你的plist包含了dictionary在array里。
因此可以用:
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
NSDictionary *dict = [array objectAtIndex:0];
或者修改plist结构
<plist version="1.0">
<dict>
<key>Title</key>
<string>Mr</string>
<key>First Name</key>
<string>xxxxxx</string>
<key>Last Name</key>
<string>yyyyyyy</string>
<key>Designation</key>
<string>Senior Engineer</string>
</dict>
</plist>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。