The ASAP cart

An e-commerce app is not complete as it only shows the products without giving the possibility of buying them.

In this section, we'll implement the cart using the same technique presented in the previous section to wrap the call to a server.

Adding a product to the cart

Let's start by defining what a cart should do, as follows:

  • Adding a product
  • Removing a product
  • Buying the cart

Given these commands, we define the gateway in this way:

import Foundation

protocol CartGateway {
    func addProductID(productID: String)
    func removeProductID(productID: String)
    func buy()
}

The local implementation is basically an empty implementation of the protocol:

class LocalCartGateway: CartGateway { func addProductID(productID: String){ } func removeProductID(productID: ...

Get Swift 2 By Example 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.