Swift UserDefault — Save Custom Object in 3 Steps ONLY!

Swift UserDefault — Save Custom Object in 3 Steps ONLY!

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. StringIntDoubleDataArray , Bool & 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 fromUserDefaults directly.

We only need 3 main steps to implement both the saving and parsing of custom object. Let’s start coding!

Final goal

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.

Step-by-Step guidelines

Step 1) Object declaration — Conform to Codable protocol

Codable is a type alias for Encodable and Decodable protocols. Object has to conform to Codable in order to be encoded to and decoded from Data format by JSONEncoder and JSONDecoder.

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._

userdefaults ios swift swift-programming ios-app-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

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.

Best Swift App Development Company

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.

Hire iOS App Developer

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...

Which company is best for iOS application development?

Hire the best iOS mobile app development company in New York, AppClues Infotech specialized in custom iOS app development and design services.

iOS(iPhone OS) App Development 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.