Nil is not compatible with expected argument type Selector

心已入冬 提交于 2019-12-31 03:37:26

问题


In converting from Swift 2.3 to Swift 3, I receive the error above for the following line of code:

var contactButton: UIBarButtonItem {return self.CustomRightItem("icon-nav-nls-contact", target: self, action: nil)}

The problem is on the nil action. I've tried using and empty selector: #selector() and ```#selector(nil) both to no avail.

How can I handle a nil action in Swift 3?


回答1:


This is clearly placeholder code for a later action, so use a placeholder function where the code will eventually go and then call that selector as required with: #selector(functionName(_:))

Your full code will look like this:

var contactButton: UIBarButtonItem {return self.CustomRightItem("icon-nav-nls-contact", target: self, action: #selector(contactAction(_:)))}

func contactAction(_ sender: AnyObject) {
    return
}


来源:https://stackoverflow.com/questions/40431812/nil-is-not-compatible-with-expected-argument-type-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!