简体   繁体   English

获取单个数组项目

[英]Get individual array Items

I am reading data from a firebase DB and storing it in a message object, How can I then access each element in that array? 我正在从Firebase数据库读取数据并将其存储在消息对象中,然后如何访问该数组中的每个元素? ie how can I use the City string as I wish to assign that to a label. 即我如何使用城市字符串,因为我希望将其分配给标签。 Same with each other element in the array. 数组中的其他元素相同。

    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)

            }


        }
    }

I will need to access each element as I have another function which will take each element and place it on a label in my ViewController 我将需要访问每个元素,因为我有另一个函数将每个元素放在我的ViewController的标签上

Something like this is what I wish to have 我希望拥有这样的东西

func DisplayAddress(){
 city.text = city
 postCode.text = postCode
}

I may be totally reading the question wrong but in trying to read into your question, I think the terminology may be where clarification is needed; 我可能完全错读了这个问题,但是在尝试读您的问题时,我认为可能需要对术语进行澄清; Object vs Array 对象与数组

An object has properties - lets examine the Details() object 一个对象具有属性-让我们检查Details()对象

var msgObj = Details()

which contains address information for one user. 其中包含一个用户的地址信息。 So conceptually this is how it would be represented in FireStore 所以从概念上讲,这就是它在FireStore中的表示方式

users
  this_user
    address
      city: "some city"
      country: "some country"
      county: "some county"
      line1: "line one"

the 'documents' are the items stored within the address collection “文档”是地址集中存储的项目

  city: "some city"
  country: "some country"
  county: "some county"
  line1: "line one"

and your Details() object has properties that correspond to those documents and stores them as properties within the object; 并且您的Details()对象具有与那些文档相对应的属性,并将它们存储为对象内的属性; city, county etc 市,县等

 msgObj.city = city
 msgObj.country = country

On the other hand, an array contains a series of objects , not properties. 另一方面, 数组包含一系列对象 ,而不是属性。 eg an array would generally not contain city, country etc, but it would contain a series of Details() objects and each of those Detail() objects has it's properties of city, country etc. For example, suppose you want to work with addresses of several different users - you would create a Details() object for each user, which contains their address information and append each one to an array. 例如,一个数组通常包含城市,国家等,但是它将包含一系列的Details()对象,并且每个Detail()对象都具有城市,国家等的属性。例如,假设您要使用地址个不同的用户-您将为每个用户创建一个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

You could then, for example, display the users within a certain radius of this user, or send them all an email etc. 然后,您可以例如在该用户的一定半径内显示用户,或向他们发送所有电子邮件等。

To answer your question, if you are working with a single users address information there is no need for an array, you can simply store it as a single Details() object variable within the class. 要回答您的问题,如果您使用的是单个用户的地址信息,则不需要数组,您可以将其存储为类中的单个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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将字符串拆分为数组,其中单个项目可能包含定界符 - Splitting a string into an array, where individual items may contain the delimiter 是否可以从数组中的一组项目中获取 rangeOfString? - Is it possible to get a rangeOfString from a group of items in an array? 如何使用NSPredicate CONTAINS IN数组获取所有项目 - How to get all items with NSPredicate CONTAINS IN array 如何根据存储在另一个数组中的索引获取数组的项 - How to get the items of an array based on the indexes stored in another array UICollectionView在过渡期间为单个项目设置动画 - UICollectionView animate individual items during transition 如何将可选字典中的项目转换为单个字符串 - How to convert items in optional Dictionary to individual strings 调整UIImageViews数组中单个图像的大小 - Resize individual images in array of UIImageViews 在swift中使用延迟调用数组项 - 获取UIResponder错误 - call array items with a delay using in swift - get UIResponder error 如何从另一个.m文件中的数组中获取项目? - How to get items from an array in a different .m file? 在Firebase中获取个人用户详细信息 - Get individual user details in firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM