Creating the Item Class

Your table view needs some rows to display. Each row in the table view will display an item with information such as a name, serial number, and value in dollars.

Create a new Swift file named Item. In Item.swift, define the Item class and give it four properties.

Listing 9.2  Creating the Item class (Item.swift)

import Foundation
import UIKit

class Item {
    var name: String
    var valueInDollars: Int
    var serialNumber: String?
    let dateCreated: Date
}

Notice that serialNumber is an optional String, necessary because an item may not have a serial number. Also, notice that none of the properties has a default value. Without these default values, Xcode will report an error that Item has no initializers. You will ...

Get iOS Programming: The Big Nerd Ranch Guide, 7th Edition 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.