使用其他关注用户的用户名填充表

时间:2015-07-26 02:05:39

标签: swift parse-platform

class FollowingTableTableViewController: UITableViewController {

let usernames = NSMutableArray()


override func viewDidLoad() {
    super.viewDidLoad()
    getUsernames()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

func getUsernames() {

    var user1 = PFUser.currentUser()
    var findUsernames:PFQuery = PFQuery(className: "FollowRequests")
    findUsernames.whereKey("from", equalTo: user1!)
    findUsernames.includeKey("to")

    findUsernames.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in

        var myObjects = objects as! [PFObject]

        //Solve this part. Need to get all of the users from the "to" field in FollowRequests.

        //If No error
        if (error == nil) {
            for myObjects in objects! {
                var user2 = PFUser()
                user2.objectForKey("to")
                self.usernames.addObject(user2.username!)
                println(user2.username)
            }
            self.tableView.reloadData()
        }

    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.usernames.count
}

override func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath)
    -> UITableViewCell {
        let item: AnyObject = self.usernames[indexPath.row]

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
        cell.textLabel!.text = (usernames[indexPath.row] as! String)
        return cell
}

我不断收到致命错误:

  

在展开Optional值时意外发现nil。

我需要做什么?

希望找到我搞砸的地方。

1 个答案:

答案 0 :(得分:0)

有很多?在你的代码中:

首先:您是否尝试使用currentUser(用户名)或(objectID)进行查询

第二:在findObjectsInBackgroundWithBlock方法中  你需要先检查是否有错误,然后将任何对象的数组转换为PFObject

if error  == nil{
if let objects = objects as! [PFObject]{
   for oneSpecificObject in objects{
     var data = oneSpecificObject["to"] as! String  
      self.usernames.addObject(data)
     }
  self.tableView.reloadData()
}   

}

第三:在cellForRowAtIndexPath方法中 为什么你有额外的行

let item: AnyObject = self.usernames[indexPath.row]

//仔细检查您的代码然后如果您仍有问题请告诉我。