Custom Object in UserDefaults : Swift

This post was originally posted at https://agrawalsuneet.github.io/blogs/custom-object-in-userdefaults-swift/ and reposted on Medium on 13th April 2022.

What are Custom Objects and Codable?

/// A type that can convert itself into and out of an external representation.
///
/// `Codable` is a type alias for the `Encodable` and `Decodable` protocols.
/// When you use `Codable` as a type or a generic constraint, it matches
/// any type that conforms to both protocols.
public typealias Codable = Decodable & Encodable
//MARK :- Employee
struct Employee: Codable {
let employeeId: Int
let name, department: String
}
// OR//MARK :- Employee
struct Employee: Codable {
let employeeID: Int
let name, department: String
enum CodingKeys: String, CodingKey {
case employeeID = "employeeId"
case name, department
}
}

How to store custom object in UserDefaults?

Please continue reading at https://agrawalsuneet.github.io/blogs/custom-object-in-userdefaults-swift/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store