UserDefaults in iOS with Swift 5

UserDefaults in iOS with Swift 5
May 16, 2020 2 Comments Development Pushpendra Kumar

Hi, Guys thanks for visiting this tutorial. In this tutorial, I am explaining to you each and everything about the UserDefaults and it’s functionality. This video I have explained to you in the Swift 5 programming language. This very simple tutorial video. Please watch to the end and share your feedback regarding this video. And do not forget to subscribe to my YouTube channel for further updates.

UserDefaults in iOS

The UserDefaults class provides a programmatic interface for interacting with the default system. The default system allows an app to customize its behavior to match a user’s preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed. Apps store these preferences by assigning values to a set of parameters in a user’s default database. The parameters are referred to as defaults because they’re commonly used to determine an app’s default state at startup or the way it acts by default.

Important
Don’t try to access the preferences subsystem directly. Modifying preference property list files may result in loss of changes, delay of reflecting changes, and app crashes. To configure preferences, use the defaults command-line utility in macOS instead.

Apple developer support

So now let’s start with the development. So for starting the development watch the following video.

Great 😊😊😊 after that found the essential code is below.

Store information in UserDefaults

UserDefaults.standard.set(name.text!, forKey: "NAME")

So, the Above line is used for storing the string data inside the UserDefaults with the identifier NAME. After that which information we have stored with the identifier NAME, We will retrieve that information by the following line of code.

Read data from User Defaults

let name = UserDefaults.standard.string(forKey: "NAME")

Create, You have done with the first step. Now secondly, I will tell you the way to store the class mode data inside user defaults. So, For that, I need to create a model class that is given below.

//MARK: The class created for model experience as shown in the video.
//MARK: UserDefaults
class User : Codable {
    var id : String?
    var name : String?
    var username : String?
    var password : String?
}

Great 😊😊😊 After that we have to store User class data in UserDefaults in iOS with swift 5 programming language. So, Get the code below.

//MARK: CREATE USER() OBJECT
        
let model = User()
model.id = "1"
model.name = name.text! //NAME
model.username = username.text //USERNAME
model.password = password.text //PASSWORD
        
//MARK: ENCODE AND STORE INFORMATION
UserDefaults.standard.set(try? PropertyListEncoder().encode(model), forKey: "USER")

After that we can read this dat by the following code of line.

In Conclusion, You have done with this tutorial. And hope now the concept of UICollectionView in UITableView in iOS with Swift 5. If you have any doubts regarding the UICollectionView in UITableView in iOS with Swift 5 then you can comment into the comment box. And if you like this tutorial with a complete description of the simple and attractive tutorial in iOS then please like my video on YouTube and do not forget to subscribe to my YouTube channel. Because you will get lots of videos related to swift development with very sort tricks.

Besides, If you are more curious about the development and knowing about tutorials then follow the below links 👇🏻👇🏻👇🏻😎

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.
Leave Comment
  1. 1

    buy cialis

    Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to more added agreeable from you!
    By the way, how can we communicate?

    Reply
    1. 1

      Pushpendra Kumar

      You can reach me on sainipushpendra51@gmail.com

      Reply

Leave a reply

Your email address will not be published. Required fields are marked *