Rob*_*bby 1 object ios firebase swift google-cloud-firestore
我在很新Firestore和Firebase图书馆。有人可以解释如何从我的字段中检索字段object并将其存储在代码中的变量中吗?我想访问所有latMin,latMax,lonMin和lonMax在我公司的FireStore每个对象,以及如果是检索每个字段名称相同的方式(例如,latMin从一个地方1和2)。我不知道这是否可能,也许还有一种更好的方式组织我的计划Firebase。
这是我的Cloud Firebase:这是我的Firestore的图像,在注释中要求。
place 1: //This is an Object in my Firestore
//Field = Number : Value ----> This is how object is orginazed
latMax : 39.727
latMin : 39.726
lonMax : -121.7997
lonMin : -121.8003
place 2:
latMax : 39.7559
latMin : 39.755
lonMax : -122.1988
lonMin : -122.1992
Run Code Online (Sandbox Code Playgroud)
我能够毫无问题地访问对象,这是我用来读取Firestore上的文档的功能:
import UIKit
import FirebaseFirestore
import Firebase
import CoreLocation
class SecondViewController: UIViewController, CLLocationManagerDelegate {
//Creating access to locationManager
var locationManager : CLLocationManager!
@IBOutlet weak var latLabel: UILabel!
@IBOutlet weak var lonLabel: UILabel!
@IBOutlet weak var place: UILabel!
@IBOutlet weak var placeImage: UIImageView!
//Storing the pass data that we got from the firt View
var lonStore = String()
var latStore = String()
var fireLon = String()
var fireLat = String()
var lonNumberStore = Double()
var latNumberStore = Double()
var fireLonMax = Double()
var fireLatMax = Double()
var fireLonMin = Double()
var fireLatMin = Double()
override func viewDidLoad() {
//Here goes some code to display on the SecondViewController
readArray()
}
func readArray() {
let placeRef = Firestore.firestore().collection("places")
placeRef.getDocuments { (snapshot, error) in
guard let snapshot = snapshot else {
print("Error \(error!)")
return
}
for document in snapshot.documents {
let documentId = document.documentID
let latMax = document.get("latMax") as! String //Getting a nil error
print(documentId, latMax) //This print all objects
}
}
}
Run Code Online (Sandbox Code Playgroud)
我丢失了一些东西,我知道,问题是我找不到在这种情况下需要做的任何参考,我已经阅读了50遍文档,却找不到解决方案。感谢您抽出宝贵的时间阅读我的帖子。
您的readArray代码已超级关闭。只需添加代码即可阅读文档中的各个字段
func readArray()
self.db.collection("places").getDocuments { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in snapshot!.documents {
let docId = document.documentID
let latMax = document.get("latMax") as! String
let latMin = document.get("latMin") as! String
let lonMax = document.get("lonMax") as! String
let lonMin = document.get("lonMin") as! String
print(docId, latMax, latMin, lonMax, lonMin)
}
}
}
Run Code Online (Sandbox Code Playgroud)
原始问题中的问题是数据库的结构。这是一种结构,它将与我的答案中的代码匹配,并且更适合OP想要完成的工作。
问题中的结构在一个集合下列出了多个位置-可以,但是可以将这些位置分组在一起。如果我们将其更改为每个集合仅占一个位置,则可以对其进行迭代并更轻松地获取数据。
| 归档时间: |
|
| 查看次数: |
6390 次 |
| 最近记录: |