首页 文章

无法将'PFObject'类型的值转换为'NSArray'

提问于
浏览
1

我有一个Parse查询返回一组用户(PFUsers) . 我将它们放在一个数组中,以便它们可以用于填充tableView . 但是,当我加载tableView时,我收到以下错误消息: Could not cast value of type 'PFObject' to 'NSArray' . 这里's the relevant code (I cut out some stuff to make it easier to read). It' s浓缩,但我可以创建一个完整的要点 .

该错误被捕获: self.realMatches = result as! [PFObject]

import UIKit

class MatchesViewController: BaseViewController {

    var realMatches: [PFObject] = []

func loadMatches() {

        if let user = self.user {

            query{
                (results: [AnyObject]?, error: NSError?) -> Void in

                if error != nil {
                    println(error)
                } else {

                    if results != nil {
                        self.matchesResults = results!
                        for result in results!{
                            if result.objectId != self.currentUser!.objectId {
                                self.realMatches = result as! [PFObject]
                            }
                        }

                        for result in results! {
                            self.user1 = result["user1"] as! PFUser
                            self.user2 = result["user2"] as! PFUser
                        }

                        self.tableView.reloadData()
                    }
                }
            }
        } else {
            println("current user doesnt exist")
        }
    }
}

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("MatchCell", forIndexPath: indexPath) as! UITableViewCell

let object = matchesResults[indexPath.row]

return cell

如何安全地将PFUser存储在用于tableView的数组中?

谢谢!!

1 回答

  • 1

    result 是使用for循环从 results 数组的数组中提取的单个PFObject .

    你应该简单地说

    self.realMatches = result as! PFObject
    

    self.realMatches 是一个数组,因此赋值也不起作用 . 您可以使用将结果附加到数组

    self.realMatches.append(result as! PFObject)
    

相关问题