Swift Dictionary [String:String] to NSMutableDictionary?

fuzzygoat picture fuzzygoat · May 20, 2015 · Viewed 16.2k times · Source

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

Answer

Airspeed Velocity picture Airspeed Velocity · May 20, 2015

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)