Oli*_*ris 2 nsarray firebase swift google-cloud-firestore
我创建了一个文档,其中包含 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")
}
}
}
Run Code Online (Sandbox Code Playgroud)
document.data()进来作为[String: Any].
只要它是Firestore 中的字符串数组
let array = document.data()["devices"] as! [String]
print(array)
Run Code Online (Sandbox Code Playgroud)
更新问题
如何附加到已创建的设备数组。
var devices = [ "Laptop",
"Tablet",
"Smartphone",
"Smartwatch",
"Games Console"]
let array = data["sub_category"] as! [String]
devices += array
print(devices)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2528 次 |
| 最近记录: |