请帮助,这个错误打破了我的应用程序。 我有两节课: 头等诗人
class Poets
{
var poetName:String = ""
var poetYearsOfLife:String = ""
var poetImage:String = ""
var poetBiography:String = ""
var books=[Works]()
}
二等工作
class Works
{
var nameWork:String = ""
var workPoet:String = ""
}
TableViewController
var poetsPath = NSBundle.mainBundle().pathForResource("Poets1", ofType: "plist")
var poetsArray = NSArray(contentsOfFile: poetsPath!)!
var booksPath = NSBundle.mainBundle().pathForResource("Works1", ofType: "plist")
var booksDict = NSDictionary(contentsOfFile: booksPath!)
for poetArray in poetsArray
{
let poet=Poets()
poet.poetName = poetArray["name"] as! String
poet.poetImage = poetArray["image"] as! String
poet.poetYearsOfLife=poetArray["yearsOfLife"] as! String
poet.poetBiography=poetArray["biography"] as! String
poets.append(poet)
poet.books = booksDict![poet.poetName as String] as! [(Works)]//<---error
for bookArray in poet.books {
var book=Works()
book.nameWork = "nameWork"
}
}
我如何修复此错误?
fatal error: NSArray element failed to match the Swift Array Element type
我有Work1.plist。我尝试创建一个segue,并从一个表中的Poets1.plist中与特定poetName关联的plist传递值。我的英语不好。
<plist version="1.0">
<dict>
<key>poet_name1</key>
<array>
<dict>
<key>nameWork</key>
<string>Благовест</string>
<key>work</key>
<string>Среди дубравы</string>
</dict>
</array>
<key>poet_name2</key>
<array>
<dict>
<key>nameWork</key>
<string>Бессонные ночи</string>
<key>work</key>
<string>Какой кошмар!</string>
</dict>
</array>
</dict>
</plist>
答案 0 :(得分:0)
我的建议是在Works
课程中添加初始值设定项:
class Works {
var nameWork = ""
var workPoet = ""
init(nameWork: String, workPoet: String) {
self.nameWork = nameWork
self.workPoet = workPoet
}
}
然后你可以像这样使用它:
let worksArray = booksDict[poet.poetName as String] as! NSArray
for work in worksArray {
let dictionaryFromArray = work as! NSDictionary
let workObject = Works(nameWork: dictionaryFromArray["nameWork"] as! String, workPoet: dictionaryFromArray["work"] as! String)
poet.books.append(workObject)
}