XCode runtime error unrecognized selector sent to instance

Hi all, I am recieving this runtime error, "[Shopping_List.AddItemViewController save:]: unrecognized selector sent to instance ..." any help would be greatly appreciated, thank you!!

//
// 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()
  }
   
  // MARK: -
  // MARK: Actions
  @IBAction func cancel(sender: UIBarButtonItem) {
    dismiss(animated: true, completion: nil)
  }
   
  @IBAction 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)
    }
  }

}

You don't where you call save, you don't show where error occurs… so hard to say…

But from your previous posts (https://developer.apple.com/forums/thread/718437) where I answered similar question, several possible causes:

You call

#selector(save(_:))

When you should call

#selector(save(sender:))

You have to declare func as objc. Not sure you use them to connect to a button.

So replace:

  @IBAction func save(sender: UIBarButtonItem) {

with

  @objc func save(sender: UIBarButtonItem) {

I don't recieve the runtime error anymore, however the item does not save into the shopping list

//
// 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
  }

}
XCode runtime error unrecognized selector sent to instance
 
 
Q