声明没有价值的字典字典

时间:2017-05-18 17:43:11

标签: swift

我想声明没有价值的字典字典。

通常我认为我应该声明可选。

但是,我无法弄清楚如何设置。

我该怎么办?

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.
    }

1 个答案:

答案 0 :(得分:1)

只需使用:var members: [String: [String:String]]?

StringString?是完全不同的类型,String?不符合协议Hashable,因此它不能用作字典中的键。< / p>