Kenny G
Kenny G

Reputation: 107

Firebase Tutorial fatal error: unexpectedly found nil while unwrapping an optional value

I was following the Swift Firebase tutorial at https://www.raywenderlich.com/109706/firebase-tutorial-getting-started and ran into a few problems.

When I try to run I get "fatal error: unexpectedly found nil while unwrapping an optional value.

I'm sorry for posting pictures instead of code but didn't know where to start and didn't want to copy/paste entire project.

Here is the complete project posted on GitHub: https://github.com/kjg531/parasnews

stacktrace

enter image description here

enter image description here

Upvotes: 0

Views: 1660

Answers (2)

Yannsonnboys
Yannsonnboys

Reputation: 11

You should write the code like this :

name = snapshot.value!["name"] as? String
addedByUser = snapshot.value!["addedByUser"] as? String
completed = snapshot.value!["completed"] as? Bool

Change the snapshot.value["blabla"] to this snapshot.value!["blabla"]. It should work n

Upvotes: 1

Amit Raj Modi
Amit Raj Modi

Reputation: 86

I think "name" key is not exist or not added on firebase so write your code like this

init(snapshot: FDataSnapshot) {
    key = snapshot.key
    name = snapshot.value["name"] as? String
    addedByUser = snapshot.value["addedByUser"] as? String
    completed = snapshot.value["completed"] as? Bool
    ref = snapshot.ref
  }

If "name" key is not exist, It will automatic set nil value to name in model class.

Upvotes: 0

Related Questions