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

Posted

技术标签:

【中文标题】如何修复:“致命错误:在展开可选值 (lldb) 时意外发现 nil”【英文标题】:How to fix: "fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)" 【发布时间】:2016-03-30 07:49:22 【问题描述】:

我正在使用 CocoaPods 开发一个 Firebase Swift 项目。

每次登录主 ViewController 后,我都会自动收到 EXC_BREAKPOINT 错误:

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

以下是我遇到错误的一些代码行:

All codes from Joke.swift:

import Foundation
import Firebase


class Joke 
private var _jokeRef: Firebase!

private var _jokeKey: String!
private var _jokeText: String!


private var _jokeVotes: Int!


private var _username: String!

var jokeKey: String 
    return _jokeKey


var jokeText: String 
    return _jokeText


var jokeVotes: Int 
    return _jokeVotes //1


var username: String 
    return _username


// Initialize the new Joke

init(key: String, dictionary: Dictionary<String, AnyObject>) 
    self._jokeKey = key

    // Within the Joke, or Key, the following properties are children

    if let votes = dictionary["votes"] as? Int 
        self._jokeVotes = votes
    

    if let joke = dictionary["jokeText"] as? String 
        self._jokeText = joke
    

    if let user = dictionary["author"] as? String 
        self._username = user
     else 
        self._username = ""
    

    // The above properties are assigned to their key.

    self._jokeRef = DataService.dataService.JOKE_REF.childByAppendingPath(self._jokeKey)




// Add or Subtract a Vote from the Joke.

func addSubtractVote(addVote: Bool) 

    if addVote 
        _jokeVotes = _jokeVotes + 1
     else 
        _jokeVotes = _jokeVotes - 1
    

    // Save the new vote total.

    _jokeRef.childByAppendingPath("votes").setValue(_jokeVotes)

   

在 JokeCellTableViewCell.swift 中:

var joke: Joke!
...............

func configureCell(joke: Joke) 

        self.joke = joke

        // Set the labels and textView.

        self.jokeText.text = joke.jokeText
        self.totalVotesLabel.text = "Total Votes: \(joke.jokeVotes)" // 2
        self.usernameLabel.text = joke.username

        // Set "votes" as a child of the current user in Firebase and save the joke's key in votes as a boolean.

.........

在主 ViewController 中,JokesFeedTableViewController.swift:

var jokes = [Joke]()

....................


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 


    let joke = jokes[indexPath.row]

    // We are using a custom cell.

    if let cell = tableView.dequeueReusableCellWithIdentifier("JokeCellTableViewCell") as? JokeCellTableViewCell 

        // Send the single joke to configureCell() in JokeCellTableViewCell.

        cell.configureCell(joke) // 3

        return cell

     else 

        return JokeCellTableViewCell()

    
   ...........

// 1, // 2, // 3 是出现错误的代码行。

希望你能帮我解决这个问题!

提前谢谢你!

【问题讨论】:

_jokeVotes 是什么? fatal error: unexpectedly found nil while unwrapping an Optional value的可能重复 @originaluser2 var jokeVotes: Int return _jokeVotes _jokeVotes 被声明为 Init @Fonix,不一样。 你在哪里初始化_jokeVotes?你是否设置了断点并使用调试器单步执行并查看 nil 是什么? 【参考方案1】:

你的问题是你没有明确定义Joke类的期望。

您的初始化程序建议Joke 上的属性应该是可选的,但是,您使用它们时就好像它们不是。您必须决定要采取哪种方式。

如果属性可以是可选的,我建议这样:

class Joke 
    private let jokeReference: Firebase

    let jokeKey: String

    private(set) var jokeText: String?

    private(set) var jokeVotes: Int?

    let username: String

    // Initialize the new Joke

    init(key: String, dictionary: Dictionary<String, AnyObject>) 
        jokeKey = key

        // Within the Joke, or Key, the following properties are children

        if let votes = dictionary["votes"] as? Int 
            jokeVotes = votes
        

        if let joke = dictionary["jokeText"] as? String 
            jokeText = joke
        

        if let user = dictionary["author"] as? String 
            username = user
         else 
            username = ""
        

        // The above properties are assigned to their key.

        jokeReference = DataService.dataService.JOKE_REF.childByAppendingPath(jokeKey)
    

但是,如果属性永远不应该是nil,则需要这样的内容:

class Joke 
    private let jokeReference: Firebase

    let jokeKey: String

    let jokeText: String

    let jokeVotes: Int?

    let username: String

    // Initialize the new Joke

    init?(key: String, dictionary: Dictionary<String, AnyObject>) 

        jokeKey = key

        guard let votes = dictionary["votes"] as? Int,
            joke = dictionary["jokeText"] as? String else 
                return nil
        

        jokeText = joke
        jokeVotes = votes

        if let user = dictionary["author"] as? String 
            username = user
         else 
            username = ""
        

        // The above properties are assigned to their key.
        jokeReference = DataService.dataService.JOKE_REF.childByAppendingPath(jokeKey)
    

【讨论】:

你的意思是用类似的东西替换它:'private var jokeRef:Firebase!私人(设置) var jokeKey:字符串!私人(设置) var jokeText:字符串! private(set) var jokeVotes: Int! private(set) var username: String!'?

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

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

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

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

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

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

使用 Swift 和 Parse 获取 Facebook UserData 时出现致命错误