Challenge B: T-Shirt store — class or struct?

Your challenge here is to build a set of objects to support a T-shirt store. Decide if each object should be a class or a struct, and why.

  • TShirt: Represents a shirt style you can buy. Each TShirt has a size, color, price, and an optional image on the front.
  • User: A registered user of the t-shirt store app. A user has a name, email, and a

ShoppingCart (see below).

  • Address: Represents a shipping address, containing the name, street, city, and zip code.
  • ShoppingCart: Holds a current order, which is composed of an array of TShirt that the User wants to buy, as well as a method to calculate the total cost. Additionally, there is an Address that represents where the order will be shipped.

Bonus: After you’ve decided on whether to use a class or struct for each object, go ahead and implement them all!

The previous chapter introduced you to the basics of defining and using classes in Swift. Classes are, in general, much more sophisticated types than their struct counterparts.

Classes introduce inheritance, overriding, and polymorphism which give them added abilities compared to structs. This in turn requires consideration for initialization, class hierarchies, and understanding the class lifecycle in memory.

This chapter will introduce you to the finer points of classes in Swift, and help you understand how you can create, use and manage complex classes.

results matching ""

    No results matching ""