[英]Could not cast value of type 'Swift._SwiftDeferredNSDictionary<Swift.String, Swift.String>' to 'NSMutableDictionary'
I have an App written in Swift 3.0 and I declared the following data types: 我有一个用Swift 3.0编写的应用程序,我声明了以下数据类型:
var movies = [Movie]()
var getPlist = NSMutableDictionary()
var movieItems = NSMutableDictionary()
And I have the following method which is loading the content of a plist: 我有以下方法来加载plist的内容:
// Connect to plist and get the data
if let plist = PlistHandler(name: "MovieData") {
getPlist = plist.getMutablePlistDict()!
// Load the movie items into the table view data source
for i in 0..<getPlist.count {
movieItems = (getPlist.object(forKey: "Item\(i)") as! NSMutableDictionary) as! [String: String] as! NSMutableDictionary
let newName = movieItems.object(forKey: "Name")
let newRemark = movieItems.object(forKey: "Remark")
if newName as? String != "" {
movies.append(Movie(name: newName as? String, remark: newRemark as? String)
)}
}
} else {
print("Unable to get Plist")
}
It calls a method called getMutablePlistDict() from another class: 它从另一个类调用一个名为getMutablePlistDict()的方法:
// Get the values from plist -> MutableDirectory
func getMutablePlistDict() -> NSMutableDictionary? {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destPath!) {
guard let dict = NSMutableDictionary(contentsOfFile: destPath!) else { return .none }
return dict
} else {
return .none
}
}
When I run the App I get the error above (see question title). 当我运行该应用程序时,出现上述错误(请参阅问题标题)。 But this is new.
但这是新的。 In Xcode 8 I didn't get this error.
在Xcode 8中,我没有收到此错误。 What is the reason for this and how I have to change my code to avoid that?
这是什么原因?如何避免更改代码?
You can use like this : 您可以这样使用:
Changed NSMutableDictionary
to [String: Any]
: 将
NSMutableDictionary
更改为[String: Any]
:
var movies = [Movie]()
var getPlist: [String: Any] = [:]
var movieItems: [String: Any] = [:]
func getMutablePlistDict() -> [String: Any] {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destPath!) {
if let dict = NSDictionary(contentsOfFile: destPath!) as? [String: Any] {
return dict
}
} else {
return [:]
}
}
if let plist = PlistHandler(name: "MovieData") {
let getPlist = plist.getMutablePlistDict()
// Load the movie items into the table view data source
for i in 0..<getPlist.count {
if let movieItemsCheck = getPlist["Item\(i)"] as? [String: Any] {
movieItems = movieItemsCheck
if let newName = movieItems["Name"] as? String, let newRemark = movieItems["Remark"] as? String, newName != "" {
movies.append(Movie(name: newName, remark: newRemark))
}
}
}
} else {
print("Unable to get Plist")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.