Beginner question in XCode "Cannot convert value of type 'String' to expected argument type 'NSNotification.Name?'"

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)
  }
Answered by BabyJ in 733654022

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.

As I told you, your tutorial is so old that many API are deprecated.

You dont't show updateShoppingList, so not sure for selector.

It is also better to define the name as an extension of Notification.Name

extension Notification.Name {  // Just better to create a name
    public static let kShoppingChanged = Notification.Name("ShoppingListDidChangeNotification")
}

        NotificationCenter.default.addObserver(self, selector: #selector(updateShoppingList), name: .kShoppingChanged, object: nil) 

// Or

        NotificationCenter.default.addObserver(self, selector: #selector(updateShoppingList(_:)), name: .kShoppingChanged, object: nil) 

Thanks for the reply, I have used google and fixed all the other errors due to the API being deprecated. This is the last error I have that I wasn't able to fix.

I have added this code and i'm getting these errors now

extension Notification.Name { // Declaration is only valid at file scope
      public static let kShoppingChanged = Notification.Name("ShoppingListDidChangeNotification")
    }
    NotificationCenter.default.addObserver(self, #selector: Notification(updateShoppingList(_:)), name: .kShoppingChanged, object: nil) //'default' label can only appear inside a 'switch' statement

so I moved the declaration to the top and now it looks like this

NotificationCenter.default.addObserver(self, selector: #selector(updateShoppingList(_:)), name: .kShoppingChanged, object: nil) 

and the error is Cannot find 'updateShoppingList' in scope

this is the updateShoppingList function:

func updateShoppingList(notification: NSNotification) {
    loadItems()
  }
   
Accepted Answer

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.

Thank you for all the help everyone!

Beginner question in XCode "Cannot convert value of type 'String' to expected argument type 'NSNotification.Name?'"
 
 
Q