我怎么解决这个问题?如何修复致命错误?

时间:2015-10-29 15:31:24

标签: swift error-handling fatal-error

这是代码:

override func viewDidLoad() {
    super.viewDidLoad()
    refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
    self.tableView.addSubview(refresher)
    refresh()
    var query = PFUser.query()
    query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
        if let users = objects {
            self.usernames.removeAll(keepCapacity: true)
            self.userids.removeAll(keepCapacity: true)
            self.isFollowing.removeAll(keepCapacity: true)
            for object in users {
                if let user = object as? PFUser {
                    if user.objectId! != PFUser.currentUser()?.objectId {
                        self.usernames.append(user.username!)
                        self.userids.append(user.objectId!)
                        var query = PFQuery(className: "followers")
                        if (PFUser.currentUser() == nil) {
                            print("currentUser() is nil");
                        } else if (PFUser.currentUser()!.objectId == nil) {
                            print("PFUser.currentUser()!.objectId is nil");
                        } else if (user.objectId == nil) {
                            print("user.objectId is nil");
                        }
                        query.whereKey("follower",equalTo: PFUser.currentUser()!.objectId!)
                        query.whereKey("following", equalTo: user.objectId!)
                        query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
                            if let objects = objects {
                                if objects.count > 0 {
                                    self.isFollowing[user.objectId!] = true

                                } else {
                                    self.isFollowing[user.objectId!] = false
                                }
                            }
                            if self.isFollowing.count == self.usernames.count {
                                self.tableView.reloadData()
                            }
                        })
                    }
                }
            }
        }
    })
}

每当我尝试运行这段代码时,我会在行上出现错误,其中包含" query.whereKey"打印到这样的日志:

  

nilPFUser.currentUser()!. objectId是nilfatal错误:意外   在解包可选值时发现nil

为什么?我该如何解决?我需要帮助。请明确点。 谢谢。

同样,在解开可选值时意外地发现了一个nil。

0 个答案:

没有答案