O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Store

Then we want to store the list of Todo items in memory. To be able to achieve this, we will create a new class called TodoStore. The code is as follows:

import Vapor final class TodoStore {     static let sharedInstance = TodoStore()     fileprivate var list: [Todo] = Array<Todo>()     private init() {     } } 

For the sake of simplicity, we make this class a singleton that stores a list of Todo items. Also, we make the init method private to avoid non-shared instance initiation.

Next, we add the following methods:

func addOrUpdateItem(item: Todo) {     if self.find(id: item.todoId) != nil {         _ = update(item: item)     } else {         self.list.append(item)     } } func listItems() -> [Todo] {     return self.list } 

As the names suggest, these methods will be ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required