Member-only story

Working With Property Lists in Swift — Part 2

Gabriel Theodoropoulos
9 min readMar 5, 2021

--

In the first part of the Working With Property Lists in Swift series, I showcased through a simple example how easy it is to use plist files in order to read and save data. Using the PropertyListEncoder and PropertyListDecoder classes along with the Codable type, encoding and decoding to and from property lists is just a matter of a couple of lines.

However, there is a downside. Codable work instantly out of the box when all properties in a custom type are of basic data types; numbers, string, data, boolean, and a few more (you can find them all in a plist file). Exactly like it happens in the CustomShape sample type that I used for demonstration in the previous part and you can see right next:

struct CustomShape {
var width: CGFloat = 0
var height: CGFloat = 0
var backgroundColor: String = "000000"
var borderColor: String = "000000"
var borderWidth: CGFloat = 0
var cornerRadius: CGFloat = 0
var isInteractive: Bool = false
var patternImage: Data = Data()
}

What happens when a custom type contains properties of data types incompatible with plists and the Codable type? For example, colors in the CustomShape type are being represented with string values, and pattern image is a Data object. What if we wanted to have UIColor and UIImage objects respectively?

--

--

Gabriel Theodoropoulos
Gabriel Theodoropoulos

Written by Gabriel Theodoropoulos

An iOS & macOS app maker writing code in Swift. Author of countless programming tutorials. Content creator. https://serialcoder.dev

No responses yet