无法识别的选择器发送到实例 Xcode 9
Unrecognised selector sent to instance Xcode 9
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.
只需将 @objc
放在使用 Selector
s 调用的所有方法之上:
@objc func logout() {
print("logout")
}
@objc func home() {
print("Home")
}
中Swift4、@objc
inference is very limited。我怀疑在 Swift.
中动态创建 Selector
是不是一个好策略
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.
只需将 @objc
放在使用 Selector
s 调用的所有方法之上:
@objc func logout() {
print("logout")
}
@objc func home() {
print("Home")
}
中Swift4、@objc
inference is very limited。我怀疑在 Swift.
Selector
是不是一个好策略