Question: I want to store font size in user defaults and read it in next runs.
But in first run when I check font size using code below, it returns 0, while I expect an exception or something like that which says this value is not saved before:

```
let font_size=CGFloat(UserDefaults.standard.float(forKey: "fontsize"))

```

Now `font_size` is 0 which should be nil or throw an exception!

The Apple document says :
**The floating-point value associated with the specified key. If the key does not exist, this method returns 0.**

But I don't like **0** But I want to know if this value is stored before or not!

Answer: 
Firstly, I don't think 0 makes sense to be the value of a variable called `fontSize`. How can you have text which is size 0? So this isn't really a problem at all.

Just limit the user to select a font size that's above 0. This way, if you get a 0 from the user defaults, you will know that the value has not been set yet.

What if the valid range includes 0?

This is a brute force solution, but it works anyway.

When you save the value, always add one to it. When you get the value back, subtract one. Again, if you get 0, you know the value has not been set.
As the [documentation for UserDefaults](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/UserDefaults/AccessingPreferenceValues/AccessingPreferenceValues.html) tells you: You can (and for your purpose probably should) set default values for the values. You have to add 

```
let userDefaults = UserDefaults.standard
let appDefaults: [String:Any] = ["fontSize" : 10,
                                 "anotherUserDefaultValue" : "Not found",
                                 "firstLaunch" : true]
userDefaults.register(defaults: appDefaults)

```

to your AppDelegate didFinishLaunchingWithOptions.