How do I convert NSDictionary to Dictionary?
Asked Answered
A

3

23

I have already updated to Xcode 8 and now I need to convert my code from Swift 2 to Swift 3.

Before, when I want to convert NSDictionary to Dictionary, I just wrote the following:

let post_paramsValue = post_params as? Dictionary<String,AnyObject?>

where post_params is NSDictionary.

But now with Swift 3, I am receiving this error:

NSDictionary is not convertible to Dictionary

Why? What's changed?


Edit 1

I've also tried the following:

let post_paramsValue = post_params as Dictionary<String,Any>

But that gives this error:

'NSDictionary!' is not convertible to 'Dictionary<String, Any>'; did you mean to use as! to force downcast?


Edit 2

I've also tried the following:

let post_paramsValue =  post_params as Dictionary<String,Any>

Where I declare NSDictionary instead of NSDictionary!, but it doesn't work; I got this error:

'NSDictionary' is not convertible to 'Dictionary<String, Any>'; did you mean to use as! to force downcast?


Edit 3

I've also tried the following:

let post_paramsValue =  post_params as Dictionary<String,Any>!

But I received this error:

'NSDictionary!' is not convertible to 'Dictionary<String, Any>!'; did you mean to use as! to force downcast?

Aube answered 1/11, 2016 at 10:48 Comment(4)
tried this ? #31766375Wolfie
@UmairAfzal thank you for your comment, but why i need to go through dictionary using loop , this is not convenient , why apple need to make things harder ?Aube
Well I do not know why, did you tried ? it works or not ?Wolfie
@UmairAfzal i didn't try it ,i am pretty sure that it will work but this is not convenient . i need to do it with the best way. thanksAube
G
25
  • NSDictionary in Objective-C has always non-optional values.
  • AnyObject has become Any in Swift 3.
  • Considering the first two "rules" NSDictionary can be bridge cast to Dictionary

let post_paramsValue = post_params as Dictionary<String,Any>

If the source NSDictionary is an optional you might use as Dictionary<String,Any>? or as? Dictionary<String,Any> or as! Dictionary<String,Any> or as Dictionary<String,Any>! depending on the actual type of the NSDictionary

Georgiegeorgina answered 1/11, 2016 at 10:53 Comment(10)
thank you for your answer, but i've tried this before and it didn't work , i've edit the questionAube
Then use the forced cast operator as! since the source dictionary is optional.Georgiegeorgina
thanks again, why i can't use this : let post_paramsValue = post_params as Dictionary<String,Any>!Aube
You can use that, tooGeorgiegeorgina
no it shows error message, one minute i will add the screen shot to my answerAube
Consider to declare the NSDictionary as non-optional.Georgiegeorgina
it also doesn't work , i will add two screen shots one minute pleaseAube
i've edited my question with all your suggestions , please have a look & let me what do you think, thanksAube
Have you tried the suggested as! (with exclamation mark)? I'd avoid to use NSDictionary in Swift at all unless you have no choice.Georgiegeorgina
i did this "let post_paramsValue = post_params as? Dictionary<String,Any> " just to avoid run time exception and it works. but why the other bridge suggestion doesn't workAube
U
3

For those who have a problem with NSDictionary, simply use this extension:

Swift 3.0

extension NSDictionary {
    var swiftDictionary: Dictionary<String, Any> {
        var swiftDictionary = Dictionary<String, Any>()

        for key : Any in self.allKeys {
            let stringKey = key as! String
            if let keyValue = self.value(forKey: stringKey){
                swiftDictionary[stringKey] = keyValue
            }
        }

        return swiftDictionary
    }
}
Unopened answered 20/9, 2017 at 14:10 Comment(0)
A
0

You just need to declare the NSDictionary properly in objc

For example: NSDictionary<NSString *, NSString*> gets translated automatically to [String: String] in swift interfaces

Ashcan answered 24/3, 2020 at 12:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.