Hi Everyone,
I am trying to save an item from my grocery list. I am recieving no errors however the item does not appear in the shopping list after I click save. Any help would be greatly appreciated, thank you!!
//
// AppDelegate.swift
// Shopping List
//
// Created by Deepak Prasad on 1/10/22.
//
import UIKit
extension Notification.Name { // Declaration is only valid at file scope
public static let kShoppingChanged = Notification.Name("ShoppingListDidChangeNotification")
}
class ShoppingListViewController: UITableViewController {
let CellIdentifier = "Cell Identifier"
var items = [Item]() {
didSet {
buildShoppingList()
}
}
var shoppingList = [Item]() {
didSet {
tableView.reloadData()
}
}
// MARK: -
// MARK: View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
// Set Title
title = "Shopping List"
// Load Items
loadItems()
// Register Class
tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: CellIdentifier)
// Add Observer
NotificationCenter.default.addObserver(self, selector: #selector(updateShoppingList), name: .kShoppingChanged, object: nil) //'default' label can only appear inside a 'switch' statement
}
// MARK: -
// MARK: Table View Data Source Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shoppingList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Dequeue Reusable Cell
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath as IndexPath)
// Fetch Item
let item = shoppingList[indexPath.row]
// Configure Table View Cell
cell.textLabel?.text = item.name
return cell
}
// MARK: -
// MARK: Notification Handling
@objc func updateShoppingList(notification: NSNotification) {
loadItems()
}
// MARK: -
// MARK: Helper Methods
func buildShoppingList() {
shoppingList = items.filter({ (item) -> Bool in
return item.inShoppingList
})
}
private func loadItems() {
if let filePath = pathForItems(), FileManager.default.fileExists(atPath: filePath) {
if let archivedItems = NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? [Item] {
items = archivedItems
}
}
}
private func pathForItems() -> String? {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
if let documents = paths.first, let documentsURL = NSURL(string: documents) {
return documentsURL.appendingPathComponent("items")?.path
}
return nil
}
}
//
// AppDelegate.swift
// Shopping List
//
// Created by Deepak Prasad on 1/10/22.
//
import UIKit
protocol AddItemViewControllerDelegate {
func controller(controller: AddItemViewController, didSaveItemWithName name: String, andPrice price: Float)
}
class AddItemViewController: UIViewController {
@IBOutlet var nameTextField: UITextField!
@IBOutlet var priceTextField: UITextField!
var delegate: AddItemViewControllerDelegate?
// MARK: -
// MARK: View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(save(sender:)))
}
// MARK: -
// MARK: Actions
@IBAction func cancel(sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
@objc func save(sender: UIBarButtonItem) {
if let name = nameTextField.text, let priceAsString = priceTextField.text, let price = Float(priceAsString) {
// Notify Delegate
delegate?.controller(controller: self, didSaveItemWithName: name, andPrice: price)
// Dismiss View Controller
dismiss(animated: true, completion: nil)
}
}
}