无法将 'Swift._SwiftDeferredNSDictionary<Swift.String, Swift.String>' 类型的值转换为 'NSMutableDictionary'
Could not cast value of type 'Swift._SwiftDeferredNSDictionary<Swift.String, Swift.String>' to 'NSMutableDictionary'
我有一个用 Swift 3.0 编写的应用程序,我声明了以下数据类型:
var movies = [Movie]()
var getPlist = NSMutableDictionary()
var movieItems = NSMutableDictionary()
我有以下加载 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")
}
它从另一个 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
}
}
当我 运行 应用程序时,出现上述错误(参见问题标题)。但这是新的。在 Xcode 8 我没有得到这个错误。这是什么原因以及我必须如何更改我的代码才能避免这种情况?
你可以这样使用:
已将 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")
}
我有一个用 Swift 3.0 编写的应用程序,我声明了以下数据类型:
var movies = [Movie]()
var getPlist = NSMutableDictionary()
var movieItems = NSMutableDictionary()
我有以下加载 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")
}
它从另一个 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
}
}
当我 运行 应用程序时,出现上述错误(参见问题标题)。但这是新的。在 Xcode 8 我没有得到这个错误。这是什么原因以及我必须如何更改我的代码才能避免这种情况?
你可以这样使用:
已将 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")
}