Skip to content

Updating

Brian Mullen edited this page Dec 4, 2016 · 2 revisions

What if you already having an object and you just want to update it? There are 2 protocols (Updatable and IndexUpdatable) you can use for updating.

All the examples will use the following struct:

struct User {
     var id: Int
     var name: String
     var email: String
}

Updatable

extension User: Updatable {
     mutating func update(with data: Extractable) throws {
          id = try data.value(for: "id")
          name = try data.value(for: "name")
          email = try data.value(for: "email")
     }
}

IndexUpdatable

extension User: IndexUpdatable {
     mutating func update(with data: IndexExtractable) throws {
          id = try data.value(for: 0)
          name = try data.value(for: 1)
          email = try data.value(for: 2)
     }
}

Which one should you use?

Which one you use depends on your preference or the data structure you are provided with for updating.

Clone this wiki locally