Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取单个数组项_Ios_Arrays_Swift_Firebase_Google Cloud Firestore - Fatal编程技术网

Ios 获取单个数组项

Ios 获取单个数组项,ios,arrays,swift,firebase,google-cloud-firestore,Ios,Arrays,Swift,Firebase,Google Cloud Firestore,我从firebase DB读取数据并将其存储在消息对象中,然后如何访问该数组中的每个元素?i、 e如何使用城市字符串,因为我希望将其分配给标签。数组中的每个元素都相同 firebaseDB.collection("user").document(key).collection("address").getDocuments() { (querySnapshot, err) in if let err = err { print("Error get

我从firebase DB读取数据并将其存储在消息对象中,然后如何访问该数组中的每个元素?i、 e如何使用城市字符串,因为我希望将其分配给标签。数组中的每个元素都相同

    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
}

我可能完全看错了这个问题,但在试图理解你的问题时,我认为术语可能是需要澄清的地方;对象与数组

对象具有属性-让我们检查Details()对象

其中包含一个用户的地址信息。所以从概念上讲,这就是它在FireStore中的表示方式

users
  this_user
    address
      city: "some city"
      country: "some country"
      county: "some county"
      line1: "line one"
“文档”是存储在地址集合中的项目

  city: "some city"
  country: "some country"
  county: "some county"
  line1: "line one"
并且您的Details()对象具有与这些文档对应的属性,并将它们作为属性存储在对象中;城市、县等

 msgObj.city = city
 msgObj.country = country
另一方面,数组包含一系列对象,而不是属性。e、 g.数组通常不包含城市、国家等,但它包含一系列Details()对象,每个Details()对象都具有城市、国家等属性。例如,假设您要处理多个不同用户的地址-您将为每个用户创建一个Details()对象,其中包含它们的地址信息,并将每个地址信息附加到一个数组中

self.dataArry[0] = the Details() objects of one user
self.dataArry[1] = the Details() object of another user
self.dataArry[2] = the Details() object of a third user
例如,您可以显示该用户特定半径内的用户,或向他们发送电子邮件等

要回答您的问题,如果您使用的是单个用户地址信息,则不需要数组,只需将其存储为类中的单个Details()对象变量即可

class ViewController: UIViewController {
    var myUserAddress = Details()

    func to get this users address documents from FireStore {
        if let city = msgdata["city"] as? String {
           self.myUserAddress.city = city
        }
        if let country = msgdata["country"] as? String {
           self.myUserAddress.country = country
        }
        //remember that the properties are only valid from here on
        //as FireStore is asychronous
        self.DisplayCity()
        self.DisplayLocation()
    }

    //and then later on when you want to display those properties
    func DisplayCity() {
      let city = self.myUserAddress.city
      print(city)
    }
    func DisplayLocation() {
      let lon = self.myUserAddress.logitude
      let lat = self.myUserAddress.latitude
      //show the location on a map via lon & lat
    }

您可以使用数组的索引访问该元素,然后使用
操作符在属性后面添加该元素的参数。但是,您还没有解释如何访问它或在哪里访问它,这将帮助您获得更好的解决方案。我希望在同一个类中访问它,只是使用不同的函数:)您希望访问数组的哪个元素?我需要访问它们中的每一个元素,但使用不同的函数。然后将该逻辑添加到问题中。哪个方法需要访问哪个元素。
class ViewController: UIViewController {
    var myUserAddress = Details()

    func to get this users address documents from FireStore {
        if let city = msgdata["city"] as? String {
           self.myUserAddress.city = city
        }
        if let country = msgdata["country"] as? String {
           self.myUserAddress.country = country
        }
        //remember that the properties are only valid from here on
        //as FireStore is asychronous
        self.DisplayCity()
        self.DisplayLocation()
    }

    //and then later on when you want to display those properties
    func DisplayCity() {
      let city = self.myUserAddress.city
      print(city)
    }
    func DisplayLocation() {
      let lon = self.myUserAddress.logitude
      let lat = self.myUserAddress.latitude
      //show the location on a map via lon & lat
    }