简体   繁体   English

在iOS Swift中解析查询

[英]Parse query in iOS swift

在此处输入图片说明 Irrespective of object ids,I want to retrieve all the data in a column. 无论对象ID是什么,我都希望检索列中的所有数据。 I have 10 usernames and all their game scores. 我有10个用户名及其所有游戏得分。 I want to get all the usernames and corresponding game scores .What should be my query ? 我想获取所有用户名和相应的游戏分数。我应该查询什么? I have stored the usernames as well in the class.I want it in swift. 我也将用户名存储在类中。 TIA :) TIA :)

var messages = [String]()
var usernames = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    var query = PFQuery(className: "RescuePost")
    query.findObjectsInBackgroundWithBlock {
        (objects, error) -> Void in

        if error == nil {
            if let objects = objects {
                for object in objects {
                    self.messages.append(object["message"] as! String)
                    self.usernames.append(object["username"] as! String)
                }
            }
        }
    }
}

If you want to download all those info you will need somewhere to put it , therefore let's create a data structure using the struct 如果要下载所有这些信息,则需要在某处放置它,因此让我们使用struct创建一个数据结构

 struct myDataStructure {
        var username:String?
        var userId:String?
        var message:String?
        var Userlocation:PFGeoPoint?
    }

Now let's create an array of that struct 现在让我们创建array of that structarray of that struct

var arrayOfData = [myDataStructure]()

Your query will look like that : 您的查询将如下所示:

 func queryData()
    {
       let query = PFQuery(className: "NameOfClassYouWantToQueryFrom")
        query.findObjectsInBackgroundWithBlock { (objects :[PFObject]?, error :NSError?) -> Void in
            if error == nil
            {
                if let objects = objects
                {
                    for eachSingleUser in objects
                    {
                        let username = eachSingleUser["username"] as! String
                        let userId = eachSingleUser["userId"] as! String
                        let message = eachSingleUser["message"] as? String
                        let userLocation = eachSingleUser["location"] as? PFGeoPoint

                          var singleData = myDataStructure()
                           singleData.username = username
                           singleData.userId = userId
                           singleData.message = message
                           singleData.Userlocation = userLocation

                           self.arrayOfData.append(singleData)    
                          //<----- Since you are using UITableView don't forget to reload data
                    }
                }
            }
        }
    }

Some Explanation: 一些解释:

findObjectsInBackgroundWithBlock method is a method that returns all your data or objects from parse into an array of PFObjects. findObjectsInBackgroundWithBlock方法是一种将所有数据或对象从解析返回到PFObjects数组中的方法。 Therefore our job is to check and extract those data in order to use it :) 因此,我们的工作是检查并提取这些数据以使用它:)

       let username = eachSingleUser["username"] as! String

In this line above, eachSingleUser is one of the many objects from parse that contains all your fields such as username, userId, message, therefore by using the subscript eachSingleUser[""] we will be able to access all those fields. 在上面的这一行中, eachSingleUser是解析的许多对象之一,其中包含您的所有字段,例如用户名,userId,消息,因此,通过使用下标eachSingleUser[""]我们将能够访问所有这些字段。

Finally : 最后:

   self.arrayOfData.append(singleData)  

In this line above we are saving each single objects into the array. 在上面的这一行中,我们将每个对象保存到数组中。

All your data is being saved and can be accessed in that array. 您的所有数据都将被保存,并且可以在该阵列中进行访问。

In cellPathRowAtIndexPath() 在cellPathRowAtIndexPath()中

let data = arrayofData[indexPath.row]
 cell.textlabel.text = data.username
 cell.detailTextlabel.text = data.message

You want to get ALL objects of a class. 您想获取一个类的所有对象。 You do this easily by constructing a basic query with no constraints, meaning your query will match with all records. 通过构造没有约束的基本查询 ,您可以轻松地做到这一点,这意味着您的查询将与所有记录匹配。

In your tableview, you access the values normally for each object: 在表视图中,通常可以访问每个对象的值:

object.objectForKey("username")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM