For the More Curious: Manually Conforming to Codable

In this chapter, you took advantage of automatic conformance to the Codable protocol and did not need to implement encode(to:) or init(from:) yourself. What if you had some property that was not codable? Let’s take a look at how to implement those two methods.

The code you add in this section will conflict with later chapters, so create a copy of your LootLogger project to work in, as you do for challenges. Open Item.swift. Create a new enumeration to describe the category of an item and add a property to reference the category.

Listing 13.11  Adding a Category enumeration (Item.swift)

enum Category {
    case electronics
    case clothing
    case book
    case other
}

var category = Category.other ...

Get iOS Programming: The Big Nerd Ranch Guide, 7th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.