Swift & Parse - 我正在尝试通过查询检索用户名。
Swift & Parse - Am trying to retrieve a username with a query.
我正在尝试检索属于 post 的用户名。但是,用户名可以在另一个 class 中找到。
这是我拥有的两个 class:
Post
User
我正在 UICollectionView 中填充帖子 class 中的所有信息,但由于用户名在用户 class 中,我的查询无法正常工作。这是我的代码...
//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 对象访问 User 对象。
您的代码将如下所示:
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()
}
}
}
我正在尝试检索属于 post 的用户名。但是,用户名可以在另一个 class 中找到。
这是我拥有的两个 class: Post User
我正在 UICollectionView 中填充帖子 class 中的所有信息,但由于用户名在用户 class 中,我的查询无法正常工作。这是我的代码...
//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 对象访问 User 对象。
您的代码将如下所示:
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()
}
}
}