When I try to save to ui colors with userDefaults, Xcode gives me the error : Fatal error: Unexpectedly found nil while unwrapping an Optional value

The Mamba

I am trying to allow my app to save UIColors in settings, and when I tried to save the settings for the background color it worked. But when I add a second block of code that should allow me to save a second UIColor, it gives me the error - Fatal error: Unexpectedly found nil while unwrapping an Optional value. Can someone show me how to save the second UIColor without an error?

      // First UIColor save - Works
    var dd = UIColor(hex: UserDefaults.standard.value(forKey: "TheMainUIColour") as! String )

    UserDefaults.standard.set(dd.toHexString, forKey: "TheMainUIColour")

    let  mainBackgroundColour = UserDefaults.standard.value(forKey: "TheMainUIColour") as! String

    let color = UIColor(hex: mainBackgroundColour)

    self.view.backgroundColor = dd

    // Second UIColor Save - Doesnt Work
    let dd2: UIColor = UIColor(hex: UserDefaults.standard.value(forKey: "TheMainUIColour2") as! String )
    UserDefaults.standard.set(dd2, forKey: "TheMainUIColour2")

    let  mainBackgroundColour2 = UserDefaults.standard.value(forKey: "TheMainUIColour2") as! String

    let color2 = UIColor(hex: mainBackgroundColour2)
Sh_Khan

The problem is

   var dd2:UIColor = UIColor(hex: UserDefaults.standard.value(forKey: "TheMainUIColour2") as! String )

it reads from defaults not save , don't use ! to read from defaults as the stored value may be nil , jsut use if let to check the existence of it in defaults or not , if exists use it , if not save the value you want , wjen you open the app again it will read that stored value

   if let saved = UserDefaults.standard.string(forKey: "TheMainUIColour2") 
   {
       var dd = UIColor(hex:saved)
   }
   else
   {
        print("No saved color")

        let dd = UIColor.red

        UserDefaults.standard.set(dd.toHexString, forKey: "TheMainUIColour2")

   }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Xcode keeps returning "fatal error: unexpectedly found nil while unwrapping an Optional value", when attempting to add an impulse

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value swift

From Dev

Fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

From Dev

.reloadData() fatal error: unexpectedly found nil while unwrapping an Optional value

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value

From Dev

swift fatal error: unexpectedly found nil while unwrapping an Optional value

From Dev

reloadData() fatal error: unexpectedly found nil while unwrapping an Optional value

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value swift

From Dev

Weird "fatal error: unexpectedly found nil while unwrapping an Optional value"

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value Computation

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value (Swift)

From Dev

CLLocationManager - fatal error: unexpectedly found nil while unwrapping an Optional value

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value in didDeselectRowAt

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value - delegate

From Dev

bms-push cordova plugin on iOS gives: fatal error: unexpectedly found nil while unwrapping an Optional value

From Dev

Swift - While playing sound I get error "fatal error: unexpectedly found nil while unwrapping an Optional value"

From Dev

Xcode - Swift - NSURL : "fatal error: unexpectedly found nil while unwrapping an Optional value"

From Dev

xcode9 swift4 Fatal error: Unexpectedly found nil while unwrapping an Optional value

From Dev

Error "fatal error: unexpectedly found nil while unwrapping an Optional value" when a segue is fired

From Dev

Swift: fatal error: unexpectedly found nil while unwrapping an Optional value when initializing UIlabel value

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value when assigning a value from an array to a UILabel

From Dev

SceneKit – Fatal Error: unexpectedly found nil while unwrapping an Optional value when getting child node

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value in Swift when tried to parse JSON

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value(When adding to a array)

From Dev

'fatal error: unexpectedly found nil while unwrapping an Optional value' when reading from JSON

Related Related

  1. 1

    Xcode keeps returning "fatal error: unexpectedly found nil while unwrapping an Optional value", when attempting to add an impulse

  2. 2

    fatal error: unexpectedly found nil while unwrapping an Optional value swift

  3. 3

    Fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

  4. 4

    fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

  5. 5

    .reloadData() fatal error: unexpectedly found nil while unwrapping an Optional value

  6. 6

    fatal error: unexpectedly found nil while unwrapping an Optional value

  7. 7

    swift fatal error: unexpectedly found nil while unwrapping an Optional value

  8. 8

    reloadData() fatal error: unexpectedly found nil while unwrapping an Optional value

  9. 9

    fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

  10. 10

    fatal error: unexpectedly found nil while unwrapping an Optional value swift

  11. 11

    Weird "fatal error: unexpectedly found nil while unwrapping an Optional value"

  12. 12

    fatal error: unexpectedly found nil while unwrapping an Optional value Computation

  13. 13

    fatal error: unexpectedly found nil while unwrapping an Optional value (Swift)

  14. 14

    CLLocationManager - fatal error: unexpectedly found nil while unwrapping an Optional value

  15. 15

    fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

  16. 16

    fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

  17. 17

    fatal error: unexpectedly found nil while unwrapping an Optional value in didDeselectRowAt

  18. 18

    fatal error: unexpectedly found nil while unwrapping an Optional value - delegate

  19. 19

    bms-push cordova plugin on iOS gives: fatal error: unexpectedly found nil while unwrapping an Optional value

  20. 20

    Swift - While playing sound I get error "fatal error: unexpectedly found nil while unwrapping an Optional value"

  21. 21

    Xcode - Swift - NSURL : "fatal error: unexpectedly found nil while unwrapping an Optional value"

  22. 22

    xcode9 swift4 Fatal error: Unexpectedly found nil while unwrapping an Optional value

  23. 23

    Error "fatal error: unexpectedly found nil while unwrapping an Optional value" when a segue is fired

  24. 24

    Swift: fatal error: unexpectedly found nil while unwrapping an Optional value when initializing UIlabel value

  25. 25

    fatal error: unexpectedly found nil while unwrapping an Optional value when assigning a value from an array to a UILabel

  26. 26

    SceneKit – Fatal Error: unexpectedly found nil while unwrapping an Optional value when getting child node

  27. 27

    fatal error: unexpectedly found nil while unwrapping an Optional value in Swift when tried to parse JSON

  28. 28

    fatal error: unexpectedly found nil while unwrapping an Optional value(When adding to a array)

  29. 29

    'fatal error: unexpectedly found nil while unwrapping an Optional value' when reading from JSON

HotTag

Archive