Swift 4.0 中报错Argument of '#selector' refers to instance method 'xxx' that is not exposed to Objective-C

Swift 2.3 中给 UIBarButtonItem 添加点击事件:

let btn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = btn

在 Swift 4.0 中会报错:Argument of ‘#selector’ refers to instance method ‘action’ that is not exposed to Objective-C。解决办法有两种:
1、在该类前加上:

@objcMembers
class ViewController: UIViewController {
}

2、在方法名前加上:

@objc func action() {
        print("add")
}

参考链接在此

    原文作者:Desmond_
    原文地址: https://www.jianshu.com/p/65a48d470139
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞