我有一个tableView,其中填充了一个名为matchesResults的数组。当您点击单元格时,我想转换到包含该对象详细信息的视图。为此,我设置了以下功能:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
self.viewUser = matchesResults[indexPath.row] as! PFUser
self.performSegueWithIdentifier("UserSegue", sender: "self.viewUser")
}
但是,当我按下tableView中的单元格时,我收到以下错误:
我定义self.viewUser的上面一行Could not cast value of type 'PFObject' to 'PFUser'
。
如果我尝试将其转换为PFObject,则该应用无法运行,我收到错误Cannot assign a value of type 'PFObject' to a value of type 'PFUser!"
。
我该如何解决这个问题?
谢谢!
编辑:使用此查询创建数组(realMatches初始化为[PFObject]
):
let userQuery = PFQuery(className: "Match")
userQuery.whereKey("user1", equalTo: user)
let userQuery2 = PFQuery(className: "Match")
userQuery2.whereKey("user2", equalTo: user)
let currentUserQuery = PFQuery(className: "Match")
currentUserQuery.whereKey("user1", equalTo: PFUser.currentUser()!)
currentUserQuery.whereKey("isActive", equalTo: true)
let currentUserQuery2 = PFQuery(className: "Match")
currentUserQuery2.whereKey("user2", equalTo: PFUser.currentUser()!)
currentUserQuery2.whereKey("isActive", equalTo: true)
let query = PFQuery.orQueryWithSubqueries([userQuery, userQuery2, currentUserQuery, currentUserQuery2])
query.includeKey("user1")
query.includeKey("user2")
query.findObjectsInBackgroundWithBlock{
(results: [PFObject]?, error: NSError?) -> Void in
if error != nil {
print(error)
} else {
if results != nil {
self.matchesResults = results!
for result in results!{
if result.objectId != self.currentUser!.objectId {
self.realMatches.append(result)
编辑2:segue代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "UserSegue") {
let destinationVC = segue.destinationViewController as! UserViewController
destinationVC.user = self.viewUser as! PFUser
}
}
}
当我点击一个单元格以启动segue时,我在行Could not cast value of type 'PFObject' to 'PFUser'
上收到错误destinationVC.user = self.viewUser as! PFUser
。