so simple / hard question here.. So I want to have a list of dictionaries titles in an array, then use the array to call a random dictionary
in the textfield I want dic1 "A" ("first")
how can I get this?
let dic1 = [
"A" : "first",
"B" : "second"]
let dic2 = [
"A" : "one",
"B" : "two"
]
var dicArray = ["dic1", "dic2"]
text("\(dicArray[0])")
CodePudding user response:
Besides removing the quotes when you instantiate dicArray, you need to do more work in Text
. The way you instantiated dicArray
, you made it an [String]
, not an [[String:String]]
. The full view would be:
struct DictView: View {
let dic1 = [
"A" : "first",
"B" : "second"]
let dic2 = [
"A" : "one",
"B" : "two"
]
var dicArray: [[String:String]]
init() {
dicArray = [dic1, dic2]
}
var body: some View {
Text("\(dicArray[0]["A"] ?? "")")
}
}
Remember, because it is a dictionary, there is no guarantee that there is a key:value for "A" so you have to deal with the optional as well.