Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 无值字典的声明字典_Swift - Fatal编程技术网

Swift 无值字典的声明字典

Swift 无值字典的声明字典,swift,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

我想声明字典中没有值的字典

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

然而,我不知道如何设置

我该怎么做

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.
    }
只需使用:var成员:[String:[String:String]]


字符串和字符串?是完全不同的类型吗,字符串?不符合协议Hashable,因此不能将其用作字典中的密钥。

var members:[String:[String:String]]=[:]不能将可选类型用作字典密钥,因此请删除所有可选性,这样您就可以了