- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to save local data in a Swift app?
In Swift, there are several ways to save local data in an app, depending on the type and size of data you want to save. You can use User Defaults, Property List Serialization, Core Data, etc. In this article, you will learn about User Defaults with some examples.
User Defaults
In iOS, macOS, and watchOS, a fundamental storage mechanism called UserDefaults enables an app to store relatively small amounts of data, including user preferences or settings. You assign a value to a particular key in a key-value pair system that is used. The value may then be obtained by using the key as a reference.
Simple data types, such as text, booleans, integers, and dates, are frequently stored in User Defaults. It is simple to use and doesn't need other frameworks or libraries.
These are some of UserDefaults' main characteristics
Data Persistence − Even if the app is closed or the device reboots, UserDefaults data remains accessible.
Simple API − Its ease of use makes it simple to set and get values by key.
Fast − UserDefaults is a suitable option for tiny quantities of data because it is built for speedy reads and writes.
Security − UserDefaults data is kept safely in the app's sandbox, which means that only the program that produced it may access it.
Here are some more examples of UserDefaults use in Swift −
Example 1: Saving and Retrieving Booleans
import Foundation // Set a boolean value for the key "isDarkModeEnabled" let defaults = UserDefaults.standard defaults.set(true, forKey: "isDarkModeEnabled") // Retrieve the boolean value for the key "isDarkModeEnabled" let isDarkModeEnabled = defaults.bool(forKey: "isDarkModeEnabled") print(isDarkModeEnabled)
Output
true
This example shows how to save a boolean value to UserDefaults using the set method and the key "isDarkModeEnabled". It then retrieves the boolean value using the bool method and the same key. Finally, it prints out the retrieved boolean value.
Example 2: Saving and Retrieving an Array of Strings
import Foundation // Set an array of strings for the key "myArray" let defaults = UserDefaults.standard defaults.set(["Apple", "Banana", "Orange"], forKey: "myArray") // Retrieve the array of strings for the key "myArray" let myArray = defaults.array(forKey: "myArray") as? [String] ?? [] print(myArray)
Output
["Apple", "Banana", "Orange"]
This example shows how to save an array of strings to UserDefaults using the set method and the key "myArray". It then retrieves the array using the array method and the same key, and casts it as an array of strings. If the casting fails for some reason, it will default to an empty array. Finally, it prints out the retrieved array.
Example 3: Saving and Retrieving an Integer
import Foundation // Set an integer value for the key "myInt" let defaults = UserDefaults.standard defaults.set(42, forKey: "myInt") // Retrieve the integer value for the key "myInt" let myInt = defaults.integer(forKey: "myInt") print(myInt)
Output
42
This example shows how to save an integer value to UserDefaults using the set method and the key "myInt". It then retrieves the integer value using the integer method and the same key. Finally, it prints out the retrieved integer value.
Example 4: Saving and Retrieving a Dictionary
import Foundation // Set a dictionary for the key "myDictionary" let defaults = UserDefaults.standard defaults.set(["name": "John", "age": 30], forKey: "myDictionary") // Retrieve the dictionary for the key "myDictionary" let myDictionary = defaults.dictionary(forKey: "myDictionary") as? [String: Any] ?? [:] print(myDictionary)
Output
["name": John, "age": 30]
This example shows how to save a dictionary to UserDefaults using the set method and the key "myDictionary". It then retrieves the dictionary using the dictionary method and the same key, and casts it as a dictionary with String keys and Any values. If the casting fails for some reason, it will default to an empty dictionary. Finally, it prints out the retrieved dictionary.
Conclusion
In conclusion, UserDefaults is a fundamental storage mechanism in iOS, macOS, and watchOS. It enables an app to store little bits of information, such as user preferences or settings. It makes use of a key-value pair system, in which a value is assigned to a particular key, and the key is subsequently used to obtain the value.
UserDefaults is an excellent tool for storing basic data types since it is straightforward to use and doesn't require other libraries or frameworks. It's not intended to handle big data sets or more sophisticated data kinds, thus in certain circumstances, you might need to utilize Core Data, SQLite databases, or Property Lists as an alternative storage method.