UserDefaults are used to store data that you want to keep even if you drop the app.
It's very simple to use.
In the sample code, the initial value is set with userDefaults.register(defaults: ["KEY_LabelText": "Hello"]]
at first.
The .register ()
is very useful and puts the initial value when there is no data corresponding to the specified key.
Then the label reads the value saved by UserDefault and reflects it in the text of the label.
And when the button is pressed, it updates the value of the userdefaults and label.
Using UserDefaults to save parameters
- Selecting values using the UIPickerView
- Repeating at a certain time
- Create a collection view with custom layouts
- UDP communication in Swift and sending a string
- Removing @objc inference warnings when migrating from Swift3 to Swift4
- Pinch to zoom in and out on an image with UIScrollView
- Removing the back button in UINavigationController.