Hi everyone, im new to xcode and I have been following a tutorial online on how to create a shopping list program and I have recieved some errors. Is anyone able to give me some help or guidance please.
For more code and understanding here is the shopping list app guide I have been following: https://code.tutsplus.com/tutorials/ios-from-scratch-with-swift-building-a-shopping-list-application-2--cms-25516
The error I am recieving is this: "Cannot convert value of type 'String' to expected argument type 'NSNotification.Name?'"
Thank you in advance!
import UIKit
class ShoppingListViewController: UITableViewController {
let CellIdentifier = "Cell Identifier"
var items = [Item]() {
didSet {
buildShoppingList()
}
}
var shoppingList = [Item]() {
didSet {
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Set Title
title = "Shopping List"
// Load Items
loadItems()
// Register Class
tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: CellIdentifier)
// Add Observer
//I recieve the error in the line below
NSNotificationCenter.default.addObserver(self, selector: "updateShoppingList:", name: "ShoppingListDidChangeNotification", object: nil)
}
You have defined updateShoppingList
so that its function signature is updateShoppingList(notification:)
, but then you are trying to use it as updateShoppingList(_:)
in the selector.
You will need to change it to this:
#selector(updateShoppingList(notification:))
or more commonly and simpler:
#selector(updateShoppingList)
Also, when you write this:
NotificationCenter.default.addObserver(self, selector: #selector(updateShoppingList), name: .kShoppingChanged, object: nil)
you are saying that the method self.updateShoppingList
exists and can be run.
Make sure you have your methods in the right place.