Save and load structs and struct arrays in UserDefaults

Save and load structs and struct arrays in UserDefaults

By default Swift’s UserDefaults doesn’t provide a method for directly storing and loading structs and struct arrays. With Swift 4, Apple unveiled a brand new way of data encoding and decoding. Swift 4 introduced the Codable protocol. It provides support for class, struct and enum.

Here is a convenient UserDefaults extension to load and store structs and struct arrays. Only requirement: the object has to implement the Codable protocol.


Florian Schmidt

Senior Java EE Software Engineer, Swift Enthusiast, Techie, Managing Partner Sneed IT UG, Founder Smipty