D. Finna D. Finna - 1 month ago 4x
Swift Question

Problems with locally stored data

I am having a very weird problem. Below I will show you the code that is giving me problems. This part

if defaults == "" {
is for some reason giving me this error:
fatal error: unexpectedly found nil while unwrapping an Optional value
. I cannot understand why. Let me show you more of my code to better understand:

var defaults = NSUserDefaults.standardUserDefaults()

@IBAction func nextThing(sender: AnyObject) {
helloName = nameInput.text!
helloSurname = surnameInput.text!

if defaults == "" { // This is where the error is, found out using breakpoint`

The weird thing is that this code worked before I added Firebase to my code?

Hope you can help me as it is really frustrating.

var defaults = NSUserDefaults.standardUserDefaults()

If you want to check if key exists:

defaults.objectForKey("YourKey") == nil

If you know the type that the key contains, you can check with an object of that type, e.g. String:

defaults.objectForKey("YourKey") == ""