【问题标题】:Parse & Xcode 6.4 problems unwrapping optionals with Swift 1.2Parse & Xcode 6.4 使用 Swift 1.2 解包选项的问题
【发布时间】:2015-07-15 03:35:12
【问题描述】:

升级到 xcode 6.4 后,我无法打开可选选项。这是我在运行这行代码时遇到的错误:

query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)

致命错误:在展开可选值时意外发现 nil (lldb)

我知道,对于 xcode 的新更新,您必须更加小心地打开可选项,但我不确定我需要进行哪些更改才能使 swift 配合。我尝试了许多在 S/O 上建议的不同方法,但还没有一种方法可以工作。这是我的完整代码:

谢谢!!

import UIKit
import Parse

class TableViewController: UITableViewController {

    var usernames = [""]
    var userids = [""]
    var isFollowing = ["":false]

    override func viewDidLoad() {
        super.viewDidLoad()

       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")

                            ***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()

                                }


                            })



                        }
                    }

                }



            }



        })


    }

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

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

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


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

        cell.textLabel?.text = usernames[indexPath.row]

        let followedObjectId = userids[indexPath.row]

        if isFollowing[followedObjectId] == true {

            cell.accessoryType = UITableViewCellAccessoryType.Checkmark

        }


        return cell
    }


    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

        let followedObjectId = userids[indexPath.row]

        if isFollowing[followedObjectId] == false {

            isFollowing[followedObjectId] = true

            cell.accessoryType = UITableViewCellAccessoryType.Checkmark

            var following = PFObject(className: "followers")
            following["following"] = userids[indexPath.row]
            following["follower"] = PFUser.currentUser()?.objectId

            following.saveInBackground()

        } else {

            isFollowing[followedObjectId] = false

            cell.accessoryType = UITableViewCellAccessoryType.None

            var query = PFQuery(className: "followers")

            query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
            query.whereKey("following", equalTo: userids[indexPath.row])

            query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

                if let objects = objects {

                    for object in objects {

                        object.deleteInBackground()

                    }
                }


            })



        }

    }
}

【问题讨论】:

    标签: swift parsing null


    【解决方案1】:

    您可以使用Optional Binding 访问登录用户,以确保它不是nil,然后再将其用作查询参数。

    所以你应该换行:

    query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
    

    与:

    if let userObj = PFUser.currentUser()?.objectId {
        query.whereKey("follower", equalTo: userObj)
    }
    

    现在这将解决您的错误并防止您的应用程序崩溃,但也许您应该重新构建您的应用程序以确认用户已登录,无论何时到达此UITableViewController。

    无论如何,您仍应使用Optional Binding 来防止潜在的崩溃。

    【讨论】:

    • 我很高兴能帮上忙 :) 既然您的问题已经解决,那么请接受我发布的帖子作为您问题的答案,以向其他用户表明问题已解决 :) meta.stackexchange.com/questions/5234/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多