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]]=[:]不能将可选类型用作字典密钥,因此请删除所有可选性,这样您就可以了