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: 
`NSUserDefaults` uses `NSNumber` to keep numeric values, so you can write something like this:

```
    if UserDefaults.standard.object(forKey: "fontsize") != nil {
        let fontSize = CGFloat(UserDefaults.standard.float(forKey: "fontsize"))
        //use `fontSize`...
    } else {
        //first run...
    }

```

Or:

```
    if let fontSize = UserDefaults.standard.object(forKey: "fontsize") as? CGFloat {
        //use `fontSize`...
    } else {
        //first run...
    }

```
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.