swift - 以数组形式获取 Firebase 文档
问题描述
我创建了一个文档,其中包含 Firestore 中的一个数组。
我现在想在我的应用程序中本地存储该数组
我已经手动创建了一个数组,但是如何使用 Firestore 文档中的数据填充它?
//Manually created Array
var devices = [ "Laptop",
"Tablet",
"Smartphone",
"Smartwatch",
"Games Console"]
override func viewDidLoad() {
super.viewDidLoad()
createDevicePicker()
createToolbar()
getPickerData()
}
//Function to pull data from Firestore
func getPickerData() {
let docRef = firebaseDB.collection("devices").document("types")
docRef.getDocument { (document, error) in
if let document = document, document.exists {
//This will print out the data
var test = document.data()
print(test)
} else {
print("Document does not exist")
}
}
}
解决方案
document.data()
以[String: Any]
.
只要它是Firestore中的字符串数组
let array = document.data()["devices"] as! [String]
print(array)
更新问题
如何追加到已创建的设备数组。
var devices = [ "Laptop",
"Tablet",
"Smartphone",
"Smartwatch",
"Games Console"]
let array = data["sub_category"] as! [String]
devices += array
print(devices)
推荐阅读
- vhdl - 无法在modelsim中应用wave
- python - 为什么属性被设置但不持久
- eiffel - 埃菲尔:将参数传递给作为参数接收的代理
- c++ - 使用 CIMG 和 JAGPDF 打印网格
- r - 如何使用 if/else 语句在绘图 for 循环中绘制不同颜色的线(在 R 中)
- docker - Docker Image 没有使用 Java 8?
- python - 有没有办法使用 Python 编辑视频的元数据?
- amazon-web-services - 无法使用密钥获取清单 使用新应用程序版本部署到 beanstalk
- reactjs - 在 react-native 中从组件 A 中的组件 B 中获取值
- java - 无法构建 DatabaseInformation - 多个数据源 Spring Boot