Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGavin Hobbs
5,205 PointsUse of unresolved identifier 'quantity'
I'm getting the error Use of unresolved identifier 'quantity'
and I can't figure out what I'm doing wrong. Any help would be greatly appreciated!
ViewController.swift
//
// ViewController.swift
// VendingMachine
//
// Created by Pasan Premaratne on 12/1/16.
// Copyright © 2016 Treehouse Island, Inc. All rights reserved.
//
import UIKit
fileprivate let reuseIdentifier = "vendingItem"
fileprivate let screenWidth = UIScreen.main.bounds.width
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var totalLabel: UILabel!
@IBOutlet weak var balanceLabel: UILabel!
@IBOutlet weak var quantityLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
let vendingMachine: VendingMachine
var currentSelection: VendingSelection?
required init?(coder aDecoder: NSCoder) {
do {
let dictionary = try PlistConverter.dictionary(fromFile: "VendingInventory", ofType: "plist")
let inventory = try InventoryUnarchiver.vendingInvetory(fromDictionary: dictionary)
self.vendingMachine = FoodVendingMachine(inventory: inventory)
} catch let error {
fatalError("\(error)")
}
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupCollectionViewCells()
print(vendingMachine.inventory)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Setup
func setupCollectionViewCells() {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
let padding: CGFloat = 10
let itemWidth = screenWidth/3 - padding
let itemHeight = screenWidth/3 - padding
layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
layout.minimumLineSpacing = 10
layout.minimumInteritemSpacing = 10
collectionView.collectionViewLayout = layout
}
// MARK: - Vending Machine
@IBAction func purchase() {
if let currentSelection = currentSelection {
do {
try vendingMachine.vend(selection: currentSelection, quantity: quantity)
} catch {
// FIXME: Error handling code
}
} else {
// FIXME: Alert user to no selection
}
}
// MARK: UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return vendingMachine.selection.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? VendingItemCell else { fatalError() }
let item = vendingMachine.selection[indexPath.row]
cell.iconView.image = item.icon()
return cell
}
// MARK: - UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
updateCell(having: indexPath, selected: true)
currentSelection = vendingMachine.selection[indexPath.row]
if let currentSlection = currentSelection, let item = vendingMachine.item(forSelection: currentSelection) {
priceLabel.text = "$\(item.price)"
totalLabel.text = "$\(item.price * Double(quantity))"
}
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
updateCell(having: indexPath, selected: false)
}
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
updateCell(having: indexPath, selected: true)
}
func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
updateCell(having: indexPath, selected: false)
}
func updateCell(having indexPath: IndexPath, selected: Bool) {
let selectedBackgroundColor = UIColor(red: 41/255.0, green: 211/255.0, blue: 241/255.0, alpha: 1.0)
let defaultBackgroundColor = UIColor(red: 27/255.0, green: 32/255.0, blue: 36/255.0, alpha: 1.0)
if let cell = collectionView.cellForItem(at: indexPath) {
cell.contentView.backgroundColor = selected ? selectedBackgroundColor : defaultBackgroundColor
}
}
}
VendingMachine.swift
//
// VendingMachine.swift
// VendingMachine
//
// Created by Gavin Hobbs on 11/1/17.
// Copyright © 2017 Treehouse Island, Inc. All rights reserved.
//
import Foundation
import UIKit
enum VendingSelection: String {
case soda
case dietSoda
case chips
case cookie
case sandwich
case wrap
case candyBar
case popTart
case water
case fruitJuice
case sportsDrink
case gum
func icon() -> UIImage {
if let image = UIImage(named: self.rawValue) {
return image
} else {
return #imageLiteral(resourceName: "default")
}
}
}
protocol VendingItem {
var price: Double { get }
var quantity: Int { get set }
}
protocol VendingMachine {
var selection: [VendingSelection] { get }
var inventory: [VendingSelection: VendingItem] { get set }
var amountDeposited: Double { get set }
init(inventory: [VendingSelection: VendingItem])
func vend(selection: VendingSelection, quantity: Int) throws
func deposit(_ amount: Double)
func item(forSelection selection: VendingSelection) -> VendingItem?
}
struct Item: VendingItem {
let price: Double
var quantity: Int
}
enum InventoryError: Error {
case invalidResource
case conversionFailure
case invalidSelection
}
class PlistConverter {
static func dictionary(fromFile name: String, ofType type: String) throws -> [String: AnyObject] {
guard let path = Bundle.main.path(forResource: name, ofType: type) else {
throw InventoryError.invalidResource
}
guard let dictionary = NSDictionary(contentsOfFile: path) as? [String: AnyObject] else {
throw InventoryError.conversionFailure
}
return dictionary
}
}
class InventoryUnarchiver {
static func vendingInvetory(fromDictionary dictionary: [String: AnyObject]) throws -> [VendingSelection: VendingItem] {
var inventory: [VendingSelection: VendingItem] = [:]
for (key, value) in dictionary {
if let itemDictionary = value as? [String: Any], let price = itemDictionary["price"] as? Double, let quantity = itemDictionary["quantity"] as? Int {
let item = Item(price: price, quantity: quantity)
guard let selection = VendingSelection(rawValue: key) else {
throw InventoryError.invalidSelection
}
inventory.updateValue(item, forKey: selection)
}
}
return inventory
}
}
enum VendingMachineError: Error {
case invalidSelection
case outOfStock
case insufficientFunds(required: Double)
}
class FoodVendingMachine: VendingMachine {
let selection: [VendingSelection] = [.soda, .dietSoda, .chips, .sandwich, .wrap, .candyBar, .popTart, .water, .fruitJuice, .sportsDrink, .gum]
var inventory: [VendingSelection : VendingItem]
var amountDeposited: Double = 10.0
required init(inventory: [VendingSelection : VendingItem]) {
self.inventory = inventory
}
func vend(selection: VendingSelection, quantity: Int) throws {
guard var item = inventory[selection] else {
throw VendingMachineError.invalidSelection
}
guard item.quantity >= quantity else {
throw VendingMachineError.outOfStock
}
let totalPrice = item.price * Double(quantity)
if amountDeposited >= totalPrice {
amountDeposited -= totalPrice
item.quantity -= quantity
inventory.updateValue(item, forKey: selection)
} else {
let amountRequired = totalPrice - amountDeposited
throw VendingMachineError.insufficientFunds(required: amountRequired)
}
}
func deposit(_ amount: Double) {
}
func item(forSelection selection: VendingSelection) -> VendingItem? {
return inventory[selection]
}
}
Thanks!
- Gavin
1 Answer
Josh Byron
5,156 PointsAfter var currentSelection: VendingSelection? in the View Controller, You should create a variable named quantity and set it equal to 1 as the initial value
Hope that helps :)
Gavin Hobbs
5,205 PointsGavin Hobbs
5,205 PointsThanks Josh Byron ! Sometimes it's the smallest mistakes that are the most confusing! :p