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: 
> 
> I expect an exception or something like that which says this value is
>  not saved before
> 
> 
> 

Exceptions are not supported in Swift

> 
> But I don't like 0
> 
> 
> 

Use [registerDefaults](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html#//apple_ref/occ/instm/NSUserDefaults/registerDefaults:) to provide your own base value.

> 
> But I want to know if this value is stored before or not!
> 
> 
> 

I assume you don't allow the user to set font size 0, so if the value is 0 it means it hasn't been set yet.
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.