Szymon Mrozek Szymon Mrozek - 15 days ago 13
Swift Question

Pull to refresh Swift3

I'm having a problem with "Pull to refresh". I'm using ViewController, and TableView inside, RefreshControl is set as here bellow:

public func refresh(sender: AnyObject) {
updateData()
refreshControl.endRefreshing()
}



override func viewDidLoad() {
super.viewDidLoad()

self.tableView.dataSource = self
self.tableView.delegate = self

floatingButton.layer.cornerRadius = floatingButton.layer.frame.height/2


refreshControl.addTarget(self, action: Selector(("refresh:")), for: UIControlEvents.valueChanged)
self.tableView.addSubview(refreshControl)

}


When I'm pulling - there is an exception :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Income.MainVC refresh:]: unrecognized selector sent to instance 0x7faf1040a9e0

Answer

In Swift 3, the first parameter is no longer ignored by default. In Swift 2, your refresh method signature was refresh(_:), now it's refresh(sender:), so Selector("refresh:") won't work.

I suggest you add an underscore to your first parameter so it looks like this func refresh(_ sender: AnyObject).

I would also suggest that you use the new Selector syntax, which should look something like this #selector(refresh(_:))

If you use the new Selector syntax (you should) I think you can also keep the first argument and have it look like this #selector(refresh(sender:))