ios - Swift, nested dictionary with different type -
i need have data this, can return nsdictionary
?, please refer code below
{ "aaa": "a1", //value: string type "bbb": "b1", "ccc": "c1", "ddd": "d1", "dict2": { "zzz": "z2", //string type, nil "ttt": "t2", "kkk": null, "sss": null, } }
code:
let dict2info = ["zzz":z1, "ttt":t1, "ccc":c1, "kkk":null, "sss":null] var dict1 = dictionary<string, anyobject>() dict["aaa"] = a1 dict["bbb"] = b1 dict["ccc"] = c1 dict["dict2"] = dict2info as? anyobject println("\(dict1)") return dict1 //return parameter type "nsdictionary?" // result: [aaa: a1, bbb: b1, ccc: c1]
problem:
dict2
missing (i have checkedkey
in dictionary,dict2
not exist)- need return
nsdictionary
?
please me!
var outerdic = [string:any]() var innerdic = [string:string?]() outerdic["aaa"] = "a1" outerdic["bbb"] = "b1" outerdic["ccc"] = "c1" outerdic["ddd"] = "d1" innerdic["zzz"] = "z2" innerdic["ttt"] = "t2" innerdic["kkk"] = nil innerdic["sss"] = nil outerdic["dict2"] = innerdic println(outerdic)
Comments
Post a Comment