Is saving custom object to UserDefaults difficult and tedious? You can now save and retrieve it with only 3 main steps! Let’s see here!
Saving primitive data (e.g.
URL) to UserDefaults is a simple task but the one for a custom object is not a trivial job. In the past, developer probably needed to save an object in the format of
Dictionary and parsed it manually.
Luckily, Apple releases a new protocol Codable
. Any class / struct conforming to Codable
can be converted to and from Data
format by JSONEncoder
and JSONDecoder. We can then save and retrieve
Data to and from
We only need 3 main steps to implement both the saving and parsing of custom object. Let’s start coding!
UserDefaults syntax for saving and retrieving data is long and dull. It is better to simplify it to only one line of code (See line 2 & line 5 above). The details will be included in the “Simpler and more readable code” session later.
Bear in mind that any nested object has to also conform to `Codable_` protocol in order to convert the whole object to and from `Data_`_ and save to UserDefaults later stage._
This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.
Apple Swift App Development India: Mobile App Development India is an expert iOS Swift App Development company in India. We develop secure and robust mobile app solutions based on new Apple Swift language.
Are you looking to transform your idea into an iPhone application? Hire iPhone programmer team from **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** to ensure the best results, utilizing all the latest trends in iOS app...
Hire the best iOS mobile app development company in New York, AppClues Infotech specialized in custom iOS app development and design services.
Skenix Infotech is a leading iPhone app development company with skilled & expert iOS app developers. Hire us to get affordable iPhone app development services.