2016-03-30 13 views
0

Ich arbeite an einem Firebase Swift-Projekt mit CocoaPods.Wie zu beheben: "Schwerwiegender Fehler: unerwartet gefunden Null beim Entpacken eines optionalen Werts (lldb)"

Jedes Mal, nachdem ich in der Hauptviewcontroller einzuloggen, automatisch erhalte ich EXC_BREAKPOINT Fehler:

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

Hier einige meiner Code-Zeilen sind, wo ich Fehler habe:

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) 

    } 
} 

In 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. 

......... 
} 

Und im Haupt ViewController, JokesFee dTableViewController.swift:

// 1, // 2, // 3 sind Codezeilen, in denen Fehler auftreten.

Ich hoffe, Sie könnten mir helfen, dies zu beheben!

Vielen Dank im Voraus!

+0

Was ist '_jokeVotes '? – Hamish

+0

Mögliches Duplikat von [Schwerwiegender Fehler: unerwartet wurde beim Entpacken eines optionalen Werts null gefunden] (http://stackoverflow.com/questions/24948302/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-value) – Fonix

+0

@ originaluser2 'var WitzStimmen: Int { zurück _jokeVotes }' _jokeVotes wird als Init –

Antwort

1

Ihr Problem ist, dass Sie die Erwartungen der Klasse Joke nicht klar definiert haben.

Ihr Initialisierer schlägt vor, dass die Eigenschaften auf Joke optional sein sollten, Sie verwenden sie jedoch, als ob sie nicht sind. Sie müssen entscheiden, auf welche Weise Sie es nehmen wollen.

Wenn die Eigenschaften optional sein kann, würde ich so etwas wie dies vorschlagen:

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

Wenn jedoch die Eigenschaften nie nil sein sollte, können Sie so etwas wie dies benötigen:

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) 
    } 
} 
+0

Willst du es durch etwas ersetzen: 'private var jokeRef: Firebase! private (set) var jokeKey: String! private (set) var jokeText: String! private (set) var witz Bewertungen: Int! private (set) var Benutzername: String! '? –

+0

@RazvanJulian Schau dir meine bearbeitete Antwort an. –

Verwandte Themen