Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
Swift&;解析-我试图通过查询检索用户名。_Swift_Parse Platform_Pfquery_Pfuser - Fatal编程技术网

Swift&;解析-我试图通过查询检索用户名。

Swift&;解析-我试图通过查询检索用户名。,swift,parse-platform,pfquery,pfuser,Swift,Parse Platform,Pfquery,Pfuser,我正在尝试检索属于帖子的用户名。但是,可以在另一个类中找到用户名 我有两门课: 我正在UICollectionView中填充Posts类中的所有信息,但由于用户名在User类中,因此我的查询不起作用。这是我的密码 //QUERY LOST let querylost = PFQuery(className: "Post") querylost.order(byDescending: "createdAt") querylost.whereKey("lostfound

我正在尝试检索属于帖子的用户名。但是,可以在另一个类中找到用户名

我有两门课:

我正在UICollectionView中填充Posts类中的所有信息,但由于用户名在User类中,因此我的查询不起作用。这是我的密码

 //QUERY LOST

    let querylost = PFQuery(className: "Post")
    querylost.order(byDescending: "createdAt")
    querylost.whereKey("lostfound", equalTo: "lost")
    querylost.findObjectsInBackground { (objects, error) in

        if let posts = objects {

            for post in posts {


                self.addresslost.append(post["address"] as! String)

                self.breedlost.append(post["breed"] as! String)

                self.phonelost.append(post["phone"] as! String)

                self.imageFileslost.append(post["imageFile"] as! PFFile)

                //HERE'S THE PROBLEM - username is in User Class

                self.usernames.append(self.queryusers[post["userid"] as! String]!)

                //ENDS here

                self.lostCollectionView.reloadData()

                self.refresherLost.endRefreshing()


            }
        }
    }

    //TO SHOW DATA

    scrollView.delegate = self
    lostCollectionView.delegate = self
    lostCollectionView.dataSource = self

}

您不应该使用
userid
字段,而应该创建一个指针来放置在Post对象上。然后可以通过Post对象访问用户对象

您的代码如下所示:

let querylost = PFQuery(className: "Post")
querylost.order(byDescending: "createdAt")
querylost.include("user")
querylost.whereKey("lostfound", equalTo: "lost")
querylost.findObjectsInBackground { (objects, error) in

    if let posts = objects {

        for post in posts {


            self.addresslost.append(post["address"] as! String)

            self.breedlost.append(post["breed"] as! String)

            self.phonelost.append(post["phone"] as! String)

            self.imageFileslost.append(post["imageFile"] as! PFFile)

            //unwrap the user pointer, and get the username
            if let user = post["user"] as? PFUser, let username = user.username{
                self.usernames.append(username) 
            }

            //ENDS here

            self.lostCollectionView.reloadData()

            self.refresherLost.endRefreshing()


        }
    }
}