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

时间:2016-03-30 07:49:23

标签: ios swift firebase cocoapods social-networking

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

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

  

致命错误:在展开Optional值时意外发现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是出现错误的代码行。

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

提前谢谢!

1 个答案:

答案 0 :(得分:1)

您的问题是您没有明确定义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) } } ,则需要这样的内容:

{{1}}