Blueprinting the Shopping List Item model
Now that we understand the files and folders in our projects a little bit, let's start writing some code. We will begin by writing code for our first model, the Shopping List Item. To do so, perform the following steps:
- Create a new group called
Modelsunder theShoppingListfolder in your project. - Then right-click and click on
New File...under theModelsfolder and select a Swift file from the iOS template. Call this new fileItem.swiftand clickCreate.
- Copy the following code into the
Item.swiftfile:
import UIKit
class Item {
var name: String
var isChecked: Bool
init(name: String, isChecked: Bool = false) {
self.name = name
self.isChecked = isChecked
}
}Let's go over the code in more detail:
We define a class calledItemwhich will serve as a blueprint for our Shopping List Items:
class Item {We then define two properties to store a name for the item and the state of the item on whether it is checked or unchecked. These...