I have list of items to display in table view. And on clicking of each item i want to trigger a method. i-e
var menuItems = [LeftMenuItem]() // -> Define at top of vie controller
func populateLeftMenuData() { // populating data in viewDidLoad
var leftMenuItem = LeftMenuItem(imageName: "flagIcon", cellLabel : "Home" , method : "home")
menuItems.append(leftMenuItem)
leftMenuItem = LeftMenuItem(imageName: "flagIcon", cellLabel : "Logout" , method : "logout")
menuItems.append(leftMenuItem)
}
This is how data is populating in tableview. On each row i am displaying "cell label" and on clicking of cell i want to call the "method".
what i am doing is
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let leftMenuItem = menuItems[indexPath.row]
perform(Selector(leftMenuItem.method!))
}
func logout () {
print("logout")
}
func home () {
print("Home")
}
But clicking on cell it saying unrecognised selector sent to an instance. where as same thing is working fine for xCode 8 swift 3.
Just put @objc
on top of all the methods invoked with Selector
s:
@objc func logout() {
print("logout")
}
@objc func home() {
print("Home")
}
In Swift 4, @objc
inference is very limited. I doubt creating a Selector
dynamically would be a good strategy in Swift.