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")
}

参考链接在此

你可能感兴趣的:(Swift 4.0 中报错Argument of '#selector' refers to instance method 'xxx' that is not exposed to Objective-C)