我无法从plist构建数组,但不仅仅是任何数组 - 它是一个字典对象数组的数组。
这是我的plist xml文件:
<plist version="1.0">
<array>
<array>
<dict>
<key>firstName</key>
<string>Jackie</string>
<key>lastName</key>
<string>Robinson</string>
</dict>
<dict>
<key>firstName</key>
<string>Roger</string>
<key>lastName</key>
<string>Clemens</string>
</dict>
</array>
<array>
<dict>
<key>firstName</key>
<string>Ichiro</string>
<key>lastName</key>
<string>Suzuki</string>
</dict>
</array>
</array>
plist的路径如下所示:
let path = NSBundle.mainBundle().pathForResource("Roster", ofType: "plist")
let pListArray = NSArray(contentsOfFile: path)
当我打印pListArray时,我会得到这样的结果:
(
(
{
firstName = Jackie;
lastName = Robinson;
},
{
firstName = Roger;
lastName = Clemens;
}
),
(
{
firstName = Ichiro;
lastName = Suzuki;
}
)
)
当我对数据进行硬编码并打印出阵列时,我看到了所需的结果:
[[Roster.Person, Roster.Person], [Roster.Person]]
我不确定数组中的数组是否会让我失望。我尝试将数据转换为Person对象,但是得到了一个致命的错误:在解开一个Optional值时意外地发现了nil;#/ p>
for person in pListArray {
Person(firstName: person["firstName"] as String, lastName: person["lastName"] as String)
}