Swift Dictionary [String:String] to NSMutableDictionary?
Asked Answered
H

5

12

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
Halie answered 20/5, 2015 at 15:35 Comment(0)
W
42

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)
Washstand answered 20/5, 2015 at 15:43 Comment(1)
Much appreciated everyone, Thank you Airspeed, much simpler than I was expecting.Halie
C
2

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
Chufa answered 20/5, 2015 at 15:49 Comment(0)
B
1

Can you try to replace this line:

var foundationDictionary = dictionary as NSMutableDictionary

With this code:

var foundationDictionary = NSMutableDictionary(dictionary: dictionary)
Bogus answered 20/5, 2015 at 15:44 Comment(0)
P
1

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"
Pulmotor answered 4/10, 2016 at 12:23 Comment(0)
S
0

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.

Stratagem answered 17/6, 2021 at 4:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.