从[String:AnyObject]转换为不相关的类型NSMutableDictionary总是失败警告

Jit*_*dra 8 dictionary casting nsmutabledictionary ios swift

代码正在运行,但我如何沉默这种每次都出现的警告?

let parentView = self.parentViewController as! SBProfileViewController
parentView.savedDetailsModel = SBSavedUserModel(data:responseObject["data"].dictionaryObject! as! NSMutableDictionary)
Run Code Online (Sandbox Code Playgroud)

从'[String:AnyObject]'转换为不相关的类型'NSMutableDictionary'总是失败警告

SavedUserModel存储已保存的信息: -

class SBSavedUserModel : NSObject { 
var userId : String!
var firstName : String!
var lastName : String!
var imageBase64 : String!

required init ( data : NSMutableDictionary) {
    self.userId =  data.objectForKey("userId") as! String
    self.firstName = data.objectForKey("fName") as! String
    self.lastName = data.objectForKey("lName") as! String
    self.imageBase64 = data.objectForKey("image") as! String
}
Run Code Online (Sandbox Code Playgroud)

Add*_*son 5

尝试更换

responseObject["data"].dictionaryObject! as! NSMutableDictionary

有了这个:

NSMutableDictionary(dictionary: responseObject["data"].dictionaryObject!)

您可以轻松地将其转换为 NSDictionary,但是出于某种原因,当您需要 NSMutableDictionary 时,您必须使用 NSMutableDictionary(dictionary:)

编辑:请参阅@Tommy 对此问题的评论,了解为什么这是必要的。