Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
In this article, we will be discussing UserDefaults in swift from scratch to the advance.
Firstly, we would explore what UserDefaults is in Swift. After that, we will discuss how we can take advantage of the UserDefaults. We will also know how the UserDefaults works internally
So let’s dive into the article.
Firstly, we are going to discuss how UserDefaults works internally.
UserDefaults uses the .plist file to store the data in key-value pairs.
Swift uses suites/domains to save data. Each suite has its own .plist file.
By default, every app has 8 suites which are listed below.
ADD_DOMAIN(_CFPreferencesStandardDomain(appPreferences->_appName, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost));
ADD_DOMAIN(_CFPreferencesStandardDomain(appPreferences->_appName, kCFPreferencesCurrentUser, kCFPreferencesAnyHost));
ADD_DOMAIN(_CFPreferencesStandardDomain(kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost));
ADD_DOMAIN(_CFPreferencesStandardDomain(kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost));
ADD_DOMAIN(_CFPreferencesStandardDomain(appPreferences->_appName, kCFPreferencesAnyUser, kCFPreferencesCurrentHost));
ADD_DOMAIN(_CFPreferencesStandardDomain(appPreferences->_appName, kCFPreferencesAnyUser, kCFPreferencesAnyHost));
ADD_DOMAIN(_CFPreferencesStandardDomain(kCFPreferencesAnyApplication, kCFPreferencesAnyUser, kCFPreferencesCurrentHost));
ADD_DOMAIN(_CFPreferencesStandardDomain(kCFPreferencesAnyApplication, kCFPreferencesAnyUser, kCFPreferencesAnyHost));
The suites are organized into search-list, so when we perform read/write operation the search list is initialized for the first time.
We can add as many suites as we want, and customize the UserDefaults according to our requirements.
You can define your suite as below
To store the values in UserDefaults CFPreferencesSetAppValue(_:_:_:) function is used. For instance, you can declare the function as
Note: UserDefaults works best when the read operations are frequent and write operations are less.
The above code uses the PropertyListValue protocol.
You can create the structure for Key as below.
After that, you can extend the Key struct for your statically defined key values.
You can perform Write operations as
After that, you can perform the Read operation as
Similarly, We can remove or delete the saved data.
To update the value for a key in UserDefault just set another value in the same key as the Write operation.
To go more deep inside the UserDefaults you can refer to the below link
Advanced UserDefaults
Please read out more interesting articles here.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Great Product, Great Team, and Great Support Service. And if you want to add more features to the product, they can submit any idea that comes to your mind. They really care about their clients and we are really happy and honored to deal with Webkul.
Osama
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.