0
votes

I'm trying to check and see if the user has stored an entry into UserDefaults.standard.object(forKey: "foo") as! Double, however, if they haven't stored an entry then it returns nil and obviously fails. So I thought I should use an if let statement, however now it says that "Initializer for conditional binding must have Optional type" Can someone please help me understand how I can check if theres an entry, and if there's an entry then continue?

1
If you just want to check if the value exists, don't bother with a cast at all. - rmaddy

1 Answers

3
votes

In order to use if let, you have to use question mark (?) instead of exclamation mark (!). So it would be

if let test = UserDefaults.standard.object(forKey: "foo") as? Double{
//blabla
}