Book Image

Swift by Example

By : Giordano Scalzo
Book Image

Swift by Example

By: Giordano Scalzo

Overview of this book

Table of Contents (15 chapters)

Building the Todos screen

Let's move on to populate the ViewController we have just created with the proper entities.

Adding the entities

The first thing we need to do is to create the entities, which is really straightforward. Basically, we just need to map the requested fields in a struct:

struct Todo: Equatable {
    let description: String
    let list: List
    let dueDate: NSDate
    let done: Bool
    let doneDate: NSDate?

func ==(todo1: Todo, todo2: Todo) -> Bool {
    return todo1.description == todo2.description
        && todo1.dueDate == todo2.dueDate
struct List {
    let description: String

Implementing the datastore

Next, we create the datastore to handle all the operations of the entities.

For now, we only return two lists of entities:

class TodosDatastore {
    private var savedLists = Array<List>()
    private var savedTodos = Array<Todo>()

        savedLists = [
            List(description: "Personal"),