fuzzygoat
fuzzygoat

Reputation: 26223

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

Upvotes: 12

Views: 18587

Answers (5)

Er. Vihar
Er. Vihar

Reputation: 1555

I know I am so late in answering this question and one answer is already accepted, but still to help someone.

Sometime converting swift dictionary ([String : String] or [AnyHashable : String] or any other) to Objective-C NSMutableDictionary, if there are some nested objects like array or dictionaries within that will not get mutable as so when updating the values will get error of [NSDictionaryI setObject:forKey:]: unrecognised selector sent to instance

To prevent this error one can convert the object in objective-c class.

In my case I am converting a JSON string into swift dictionary and passing it to objective c class by casting it in NSMutableDictionary as per my core requirement. This gives me an above error when I try to update the NSMutableDictionary in Objective C class.

After surfing and trying much I found that if I try to convert my JSON string in NSMutableDictionary in Objective-C class itself than it worked for me.

So try this: In aVC.swift

let Str: String = nsmanagedobject.value(forKey: "<key>") as? String {
     if let mutableDict: NSMutableDictionary = ApplicationData.convertJsonObject(fromJsonString: Str) as? NSMutableDictionary {
          ......
          <Your code to pass NSMutableDictionary to respected Objective-C class>
          ......
     }

ApplicationData.h

+ (NSObject *)convertJsonObjectFromJsonString:(NSString *)jsonString;

ApplicationData.m

+ (NSObject *)convertJsonObjectFromJsonString:(NSString *)jsonString {
    NSObject *jsonObject = nil;
    if ([jsonString length] > 0) {
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        if (jsonData) {
            jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
        }
    }
    NSLog(@"jsonObject: %@",jsonObject);
    return jsonObject;
}

I hope this will help someone and save some time.

Upvotes: 0

Ved Rauniyar
Ved Rauniyar

Reputation: 1589

Dictionaries

Create immutable dictionary

let dictionary = ["Item 1": "description", "Item 2": "description"]

Create mutable dictionary

var dictionary = ["Item 1": "description", "Item 2": "description"]

Append new pair to dictionary

dictionary["Item 3"] = "description"

Upvotes: 1

Zell B.
Zell B.

Reputation: 10296

One alternative answer to the answers above is to cast your dictionary to NSDictionary create a mutable copy of it and cast it to NSMutableDictionary. Too complicated, right ? Therefore I recommend creating new NSMutableDictionary from Dictionary this is just an alternative

var foundationDictionary = (dictionary as NSDictionary).mutableCopy() as! NSMutableDictionary

Upvotes: 2

ridvankucuk
ridvankucuk

Reputation: 2407

Can you try to replace this line:

var foundationDictionary = dictionary as NSMutableDictionary

With this code:

var foundationDictionary = NSMutableDictionary(dictionary: dictionary)

Upvotes: 1

Airspeed Velocity
Airspeed Velocity

Reputation: 40963

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)

Upvotes: 42

Related Questions