0

i have a value in NSUserdefaults and every time i run the code it will print "Optional("value")" i tried several unwrapping techniques and many examples in searches yet nothing seems to solve my problem

the code

let SaveData = NSUserDefaults.standardUserDefaults()
  if(SaveData.valueForKey("newString") != nil){


        print(SaveData.valueForKey("newString") as! String!)
        print(SaveData.valueForKey("newString") as! String)
        print(SaveData.valueForKey("newString")!)

    //    SD.executeChange("INSERT INTO \(ImageIs)Array (\(ImageIs)) VALUES ('\(abc)')")

        SaveData.setValue(nil, forKey: "newString")

    }
nhgrif
  • 61,578
  • 25
  • 134
  • 173

1 Answers1

2

So, rather than checking whether something is nil, and then when you find out it's not, you both force unwrap and force cast it, we can instead pull this value out safely using optional binding and the correct NSUserDefaults method: stringForKey(_:):

if let data = NSUserDefaults.standardUserDefaults().stringForKey("newString") {
    print(data)
}

But, your second & third tries shouldn't be showing the string as optional. This playground demonstrates effectively the exact same thing as what you're doing here with NSUserDefaults.

func optionalFunc() -> Any? {
    return "Hello world."
}

print(optionalFunc())
/* unsafe */ print(optionalFunc() as! String!) /* unsafe */ 
/* unsafe */ print(optionalFunc() as! String)  /* unsafe */ 
/* unsafe */ print(optionalFunc()!)            /* unsafe */ 

if let unwrapped = optionalFunc() as? String {
    print(unwrapped)
}

![enter image description here

As you can see, only the first case prints the Optional("Hello world.") string.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • i fixed it ! thank you, the problem was the i set the value from a textbox and forgot to add ! , what gave me an Optional("Optinal("value")") – Alex.Pinhasov Mar 26 '16 at 19:50
  • 2
    You should ***ALWAYS*** *"forget"* to add the `!`. It is **completely** unsafe, and you're basically the next "Unexpectedly found nil" question waiting to happen. Please learn how to use optionals correctly. There is never an appropriate time to force cast or force unwrap. ***YOU ARE NOT A JEDI*** – nhgrif Mar 26 '16 at 19:52
  • how do i make sure the textbox was populated with text? – Alex.Pinhasov Mar 26 '16 at 19:55
  • 1
    It's up to the business logic of your app, but at this rate, if you got `nil`, the textbox would not only be not populated with text, but the user would be looking at their home screen rather than your app, which just crashed. – nhgrif Mar 26 '16 at 19:57
  • nhgrif speaks truth. You should avoid "!" forced unwrapping like the plague. That will make your app crash if the thing you are unwrapping contains nil. Use "if let" optional binding or various other techniques instead. See this thread for more information on unwrapping optionals: http://stackoverflow.com/questions/35683813/im-getting-an-error-fatal-error-unexpectedly-found-nil-while-unwrapping-an-op – Duncan C Mar 26 '16 at 21:13