swiftdictionarysprite-kitnsmutabledictionary

Swift Dictionary [String:String] to NSMutableDictionary?


I am trying to create and assign a Swift Dictionary of type [String : String] at the SKSpriteNode property userData which requires an NSMutableDictionary. When I try this I get the error:

'[String : String]' is not convertible to 'NSMutableDictionary'

Can anyone point me in the right direction?

    // USER DATA
    var dictionary: [String : String] = Dictionary()
    dictionary["Orbit"] = orbit
    dictionary["Zone"] = zone
    dictionary["Impact"] = impact
    var foundationDictionary = dictionary as NSMutableDictionary
    neoSprite.userData = foundationDictionary

Solution

  • There’s no built-in cast for this. But instead you can use NSMutableDictionary’s initializer that takes a dictionary:

    var foundationDictionary = NSMutableDictionary(dictionary: dictionary)