gpt4 book ai didi

Swift & Parse - 我尝试通过查询检索用户名。

转载 作者:行者123 更新时间:2023-11-30 11:24:27 26 4
gpt4 key购买 nike

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

这是我有的两个类(class): Post User

我正在将 Posts 类中的所有信息填充到 UICollectionView 中,但由于用户名位于 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

}

最佳答案

您应该创建一个指针来放置您的 Post 对象,而不是使用 userid 字段。然后您可以通过 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()


}
}
}

关于Swift & Parse - 我尝试通过查询检索用户名。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50920075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com