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()
.