others-how to solve `Argument passed to call that takes no arguments` swift and xcode?
1. Problem
Sometimes, when we build our iOS app in xcode, we encounter the following error:
Argument passed to call that takes no arguments
Here is the code that cause the problem:
DispatchQueue.global().async {
MessageService.shared.listMessagesByKeyword(userText: userText) { messages in //query
self.loadMessagesFromSearch(messages: messages,failedHandlerx: nil) {//reload data to buffer
DispatchQueue.main.async {
self.tableView.reloadData(doQuery: false) //this line introduce the error
}
}
}
}
the function being called:
func reloadData(doQuery: Bool = true) {
DispatchQueue.global().async {
if doQuery {
self.loadMessages(queryLatest: false,failedHandlerx: nil) {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
else {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
And the error screenshot is as follows:
You can see that the function being called DOES have a parameter, and the caller did pass a parameter, why did the error happen?
2. Solution
After googling and debugging , I found the reason of this problem, because the reloadData
function is a method of TableView, it has no parameters. I need to give the function another name.
So I changed my code to:
func reloadData1(doQuery: Bool = true) { // I renamed the function from reloadData to reloadData1
DispatchQueue.global().async {
if doQuery {
self.loadMessages(queryLatest: false,failedHandlerx: nil) {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
else {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
then call it with :
DispatchQueue.global().async {
MessageService.shared.listMessagesByKeyword(userText: userText) { messages in
self.loadMessagesFromSearch(messages: messages,failedHandlerx: nil) {
DispatchQueue.main.async {
self.reloadData1(doQuery: false) //call reloadData1 now
}
}
}
}
Now it works!
3. Summary
In this post, I demonstrated how to solve the Argument passed to call that takes no arguments
problem when using swift and xcode, the key point is to make sure your caller is calling the right function . That’s it, thanks for your reading.