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

Posted

技术标签:

【中文标题】swift:致命错误:在展开可选值(lldb)时意外发现 nil,线程 1【英文标题】:swift: fatal error: unexpectedly found nil while unwrapping an Optional value (lldb), Thread 1 【发布时间】:2015-06-06 04:31:12 【问题描述】:

我在运行模拟器时不断收到这个致命错误,我不知道如何解决它。我正在使用 Parse 创建一个模拟 Instagram 项目,由于某种原因,我收到了一个致命错误和一个 THREAD 1:

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

我知道这是一个常见问题,因为我查看了许多包含此致命错误的问题。但是,我对 Swift 编程非常陌生,所以我很困惑,如果有人能帮助我,我将不胜感激,谢谢!

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

                
            


        )



        

    

【问题讨论】:

【参考方案1】:

您可能没有以任何用户身份登录,因此PFUser.currentUser() 返回nil,当您强制解包时会产生错误,因为您无法解包 nil 值。

首先检查是否有用户使用:

if let user = PFUser.currentUser()
    println("User logged, lets do this")
    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()
            
        
    )
 else 
    println("No user logged, ops!")

【讨论】:

谢谢你是对的,我从我的 ViewController.Swift 中删除了下面的代码,所以我可以重新登录,因为当我运行模拟器时代码自动进入 tableView。一旦我登录,我所有的数据都在那里。再次感谢 override func viewDidAppear(animated: Bool) if PFUser.currentUser() != nil self.performSegueWithIdentifier("login", sender: self) @DreJames 很高兴听到,如果此答案对您有帮助,请不要忘记检查已签名的绿色支票 :)

以上是关于swift:致命错误:在展开可选值(lldb)时意外发现 nil,线程 1的主要内容,如果未能解决你的问题,请参考以下文章

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

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

AVCaptureSession 运行时崩溃。致命错误:在展开可选值 (lldb) 时意外发现 nil

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

如何修复:“致命错误:在展开可选值 (lldb) 时意外发现 nil”

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