-1

enter image description here

The program crashes once the user adds image, and username. Not sure what to do, any suggestions?

Rengers
  • 14,911
  • 1
  • 36
  • 54

1 Answers1

0

The type of the variable currentUser is String?; an optional String, which means that the value it holds at runtime can be an actual string or nil. In order to use an optional value, you need to unwrap it. In the line where you're seeing the crash, you are forcing the unwrapping with currentUser!, which will crash if the value in currentUser was in fact nil.

Looking at line 21 in your screenshot, I'm guessing that the key uid doesn't exist in the Keychain at the time of running.

Cem Schemel
  • 442
  • 3
  • 13
  • So because it may not recognize the uid, thats why its not being stored in firebase, and it crashes? How could fix it to where it doesn't it crash. – Tyrek Clarke Dec 06 '18 at 01:31
  • All I can say is that there isn't a value for the key `uid` in the keychain, hence `currentUser` is ending up `nil`. _Why_ is there not a `uid` entry in the keychain is not something I can answer here; it requires a deeper understanding of your app. But either way, I would recommend some defensive coding: what do you want your app to do when `uid` is not in the keychain? Be sure to handle that case. Because even if you put that key there yourself, it can get cleared by the user, or a system wipe, etc. It's not entirely under your control, so you can't assume it will always be there. – Cem Schemel Dec 06 '18 at 01:48
  • Weird thing is before I migrated the files over to another program, it was working perfectly and storing the users, and messages to the database. – Tyrek Clarke Dec 06 '18 at 04:20