1

我目前正在尝试读取 plist 文件并尝试在 UITableView 中显示数据,但是在读取 plist 文件时我得到了 nil 数据。

编辑

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>
*/

现在字典和数组都不包含数据这是什么原因?如何在表格视图中同时显示键和值?

4

3 回答 3

2

您的 plist 文件包含一个数组内的字典。所以要么使用

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>
于 2013-01-01T18:59:15.970 回答
1

在顶部你有一个<array>而不是<dict>。所以它应该是这样的......

NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
NSDictionary *dict=[array objectAtIndex:0];
NSArray *valueArray = [dict allKeys];
于 2013-01-01T19:00:59.140 回答
-1

在文件开头试试这个:

<?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">
于 2013-01-01T18:52:32.253 回答