gpt4 book ai didi

ios - 如何转换 Dictionary.Values?进入字符串数组 SWIFT

转载 作者:行者123 更新时间:2023-11-28 05:41:39 26 4
gpt4 key购买 nike

有没有办法转换类型为 Dictionary.Values 的数组?转换成 String 类型的一维数组?

代码:

docRef.getDocument { (document, error) in
if let document = document, document.exists {

let dataDescription = document.data()?.values // Type 'Dictionary<String, Any>.Values?'
self.array.append(dataDescription) // Tried dataDescription.values, but it doesn't work

print("Document data: \(String(describing: dataDescription))")
} else {
print("Document does not exist")
}
}

最佳答案

您也可以解包数据,然后将值从 Any 映射到 String:

docRef.getDocument { document, error in
if let document = document, document.exists,
let data = document.data()?.values {
let values = data.values.compactMap{$0 as? String}
print(values)
} else {
print("Document does not exist")
}
}
}

关于ios - 如何转换 Dictionary<String, Any>.Values?进入字符串数组 SWIFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56548045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com