whitebear
whitebear

Reputation: 12423

Declaring dictionary of dictionary without value

I want to declare dictionary of dictionary without value.

Normally I think I should declare optional.

However, I can't figure out how to set.

How can I do it???

class AppDelegate: UIResponder, UIApplicationDelegate {

    let p1: [String: String] = ["name" : "John","age" : "30"] // make p1
    let p2: [String: String] = ["name" : "Picker","age" : "32"] // make p2
    let p3: [String: String] = ["name" : "Tom","age" : "28"] // make p3
    var members: [String?: [String?:String?]] // it shows error
    ///Type 'String?' does not conform to protocol 'Hashable'

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
    {
        members  = ["key1":p1,"key2":p2,"key3": p3]
        // I want to put variables here.
    }

Upvotes: 0

Views: 55

Answers (1)

Mo Abdul-Hameed
Mo Abdul-Hameed

Reputation: 6110

Just use: var members: [String: [String:String]]?.

String and String? are completely different types, String? does not conform to protocol Hashable, so it can't be used as a key in a dictionary.

Upvotes: 1

Related Questions