O'Reilly logo

Swift 2 By Example by Giordano Scalzo

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

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: ...

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