0

I've been using Swift for awhile, but i still struggle with understanding optionals on a daily basis. It is the hardest thing for me to grasp currently.

I'm having trouble unwrapping this UserDefaults value to not include the Optional() surroundings.

Storing the value

globals.defaults.set(String(describing: cardView.backgroundColor), forKey: "bgColor")
print(String(describing: cardView.backgroundColor!))
//print this: UIExtendedGrayColorSpace 1 1

Reading the value

let tValues = def.string(forKey: "bgColor")!.components(separatedBy: " ")
print(tValues)
//print this: Optional(UIExtendedGrayColorSpace", "1", "1)

def is an instance of UserDefaults.standard. As you can see i'm force unwrapping the key when i read it. And i've tried doing it there and also at print(tValues)! instead. But they produce the same result.

How do i get rid of the Optional().

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
TheValyreanGroup
  • 3,554
  • 2
  • 12
  • 30
  • 1
    `backgroundColor` is an *optional* property, therefore `String(describing: cardView.backgroundColor)` gives the string "Optional(UIExtendedGrayColorSpace 1 1)". You *don't* unwrap the color when saving it into the defaults. – Martin R Nov 11 '16 at 23:34
  • When i print `String(describing: cardView.backgroundColor)` it is not optional. See my comment in the _Storing the value_ section – TheValyreanGroup Nov 11 '16 at 23:41
  • 2
    In your *print* statement (second line) you unwrap the color. But not in the first line where you store the color into the defaults. – Martin R Nov 11 '16 at 23:43
  • @MartinR That did it, thanks man. I thought for sure i had tried that. – TheValyreanGroup Nov 11 '16 at 23:52
  • @TheValyreanGroup http://stackoverflow.com/a/34366333/2303865 – Leo Dabus Nov 12 '16 at 02:44
  • @LeoDabus Yes, I found that extension shortly after fixing this issue and switched to using that extension. It's very nice. I found it on a different SO question though. Did you write it? I did change the one function to match swift 3 convention ;) `func color(forKey: String)` – TheValyreanGroup Nov 12 '16 at 02:51
  • @LeoDabus I found it here http://stackoverflow.com/questions/1275662/saving-uicolor-to-and-loading-from-nsuserdefaults/30576832#30576832 – TheValyreanGroup Nov 12 '16 at 02:56
  • @TheValyreanGroup I have just updated mine to Xcode 8.1 Swift 3.0.1 – Leo Dabus Nov 12 '16 at 03:04
  • `let` statement made it optional. try `let obj:[String]` or use var – Ankit Thakur Nov 12 '16 at 06:10
  • @AnkitThakur That's not what `let` means. I do know that. – TheValyreanGroup Nov 12 '16 at 06:18

1 Answers1

0

Optionals are there to protect us from the possibilities of a nil value in Swift. They could be tricky to wrap your head around and a pain to deal with at times but think of them as a shield from crashing your program. NEVER force unwrap. There is usually a better way.

Try safely unwrapping your UserDefaults object and then printing it. Something like this should work using an if let block.

if let tValues = def.string(forKey: "bgColor"){ //def.string(forKey: "bgColor") is now safely unwrapped print(tValues.components(separatedBy: " ")) }