【发布时间】:2018-10-06 12:16:21
【问题描述】:
我正在从 firebase 数据库读取数据并将其存储在消息对象中,然后我如何访问该数组中的每个元素?即如何使用 City 字符串,因为我希望将其分配给标签。与数组中的其他元素相同。
firebaseDB.collection("user").document(key).collection("address").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
}
else {
self.dataArr.removeAll()
for document in querySnapshot!.documents {
//print("\(document.documentID) => \(document.data())")
let msgdata = document.data() as! [String:Any]
var msgObj = Details()
if let city = msgdata["city"] as? String {
msgObj.city = city
}
if let country = msgdata["country"] as? String {
msgObj.country = country
}
if let county = msgdata["county"] as? String {
msgObj.county = county
}
if let lineOne = msgdata["lineOne"] as? String {
msgObj.lineOne = lineOne
}
if let lineTwo = msgdata["lineTwo"] as? String {
msgObj.lineTwo = lineTwo
}
if let postCode = msgdata["postCode"] as? String {
msgObj.postCode = postCode
}
self.dataArr.append(msgObj)
}
}
}
我需要访问每个元素,因为我有另一个函数,它将获取每个元素并将其放置在我的 ViewController 中的标签上
我希望有这样的东西
func DisplayAddress(){
city.text = city
postCode.text = postCode
}
【问题讨论】:
-
您使用它的索引访问数组的元素,并且使用
.运算符的元素的参数跟随属性。但是,您还没有解释要如何或在何处访问它,这将帮助您获得更好的解决方案。 -
我希望在同一个类中访问它只是一个不同的功能:)
-
你想访问数组的哪个元素?
-
好吧,我需要访问它们中的每一个,但功能不同。
-
然后将该逻辑添加到问题中。哪个方法需要访问哪个元素。
标签: ios arrays swift firebase google-cloud-firestore