简体   繁体   English

无法转换类型为&#39;Swift._SwiftDeferredNSDictionary&#39;的值 <Swift.String, Swift.String> ”到“ NSMutableDictionary”

[英]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.

相关问题 无法转换类型为&#39;Swift.Array的值 <Swift.Dictionary<Swift.String, Swift.String> &gt;”()到“ Swift.Dictionary” <Swift.String, Any> &#39;() - Could not cast value of type 'Swift.Array<Swift.Dictionary<Swift.String, Swift.String>>' () to 'Swift.Dictionary<Swift.String, Any>' () 无法将“SwiftyJSON.JSON”类型的值转换为“Swift.String” - Could not cast value of type 'SwiftyJSON.JSON' to 'Swift.String' 无法转换类型“Swift.Array”的值<Any> &#39; 到 &#39;Swift.Dictionary<Swift.String, Any> &#39; - Could not cast value of type 'Swift.Array<Any>' to 'Swift.Dictionary<Swift.String, Any>' 无法转换类型为&#39;Swift._NativeDictionaryStorageOwner的值 <Swift.String, Swift.AnyObject> &#39;(0x40c2134)到&#39;NSArray&#39;(0x383bb8d8) - Could not cast value of type 'Swift._NativeDictionaryStorageOwner<Swift.String, Swift.AnyObject>' (0x40c2134) to 'NSArray' (0x383bb8d8) “unsafeMutableAddressor : Swift.String”,引用自:...” - "unsafeMutableAddressor : Swift.String", referenced from:.." 类型不匹配(Swift.Dictionary<swift.string, any> ,</swift.string,> - typeMismatch(Swift.Dictionary<Swift.String, Any>, 存档iOS App时出错-“通用专业化 <Swift.String> Swift.String.init” - Error when archiving iOS App - “generic specialization <Swift.String> of Swift.String.init” 类型不匹配(Swift.Dictionary<Swift.String, Any> debugDescription: &quot;预期解码字典<String, Any>而是找到了一个数组。” - typeMismatch(Swift.Dictionary<Swift.String, Any> debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead." “NSMutableDictionary”类型的值在 swift 中没有成员“字符串” - Value of type 'NSMutableDictionary' has no member 'string' in swift 指定String(Swift)时,无法将NSURL类型的值强制转换为NSString - Could not cast value of type NSURL to NSString when String is specified (Swift)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM