Я новичок в Swift и пытаюсь запустить функцию через NotificationCenter. Наблюдатель в ViewController.swift вызывает функцию reload
:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(reload), name: NSNotification.Name(rawValue: "reload"), object: nil)
}
func reload(target: Item) {
print(target.name)
print(target.iconName)
}
... который имеет параметр класса Ítem
:
class Item: NSObject {
let name: String
let iconName: String
init(name: String, iconName: String) {
self.name = name
self.iconName = iconName
}
}
Уведомление отправлено из «menu.swift»:
class menu: UIView, UITableViewDelegate, UITableViewDataSource {
let items: [Item] = {
return [Item(name: "Johnny", iconName: "A"), Item(name: "Alexis", iconName: "B"), Item(name: "Steven", iconName: "C")]
}()
...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reload"), object: items[indexPath.row])
}
Как присвоить значение объекта items[indexPath.row]
из 'menu.swift' параметру функции reload
в 'ViewController.swift'?