0

I'm trying to create a completion block where a function can be executed but I keep getting the error:

Cannot convert value of type '()' to expected argument type '() -> Void'

Here is the function:

var labelViews : [LabelViews] = []

private func removeAllLabels(completion: (() -> Void)) {
    guard let mapController = viewModel.mapController,
        let currentMap = mapController.currentMap else {
            return
    }

    LabelViews.forEach { view in
        DispatchQueue.main.async {
            mapController.removeComponent(view, on: currentMap)
        }  
    }
    completion()
}

But when I try and use it I get error:

self.removeAllCampusLabels(completion: self.labelViews.removeAll()) // error happens here
TylerP
  • 9,600
  • 4
  • 39
  • 43
SwiftyJD
  • 5,257
  • 7
  • 41
  • 92

3 Answers3

1

You need to specify what will happen in multiple completion circumstances:

var labelViews : [LabelViews] = []



  private func removeAllLabels(completion: nil) {
                guard let mapController = viewModel.mapController,
                    let currentMap = mapController.currentMap else {
                        return
                }

                LabelViews.forEach { view in
                    DispatchQueue.main.async {
                        mapController.removeComponent(view, on: currentMap)}  
    }
                func comp(completion: () -> Void) {
                print("Success")
                completion()
          }

   }
EJZ
  • 1,142
  • 1
  • 7
  • 26
0

Assuming removeAllLabels and removeAllCampusLabels are the same methods (ignoring typo), replace the method call

self.removeAllCampusLabels(completion: self.labelViews.removeAll())

with

self.removeAllCampusLabels { self.labelViews.removeAll() }
sats
  • 647
  • 6
  • 17
-1

Your question defines a function named removeAllLabels, but then you say the error occurs when you call removeAllCampusLabels, which your question did not define. You will get better help if you check that the code you put in your question is sufficient to reproduce the error you're asking about.

I'll assume that your function is in fact named removeAllLabels.

Here's your declaration of removeAllLabels:

private func removeAllLabels(completion: (() -> Void))

This is a function that takes one argument, which is called completion. That argument must have the type () -> Void, which means the argument must itself be a function that takes no arguments and returns nothing.

Here's how (I assume) you try to call removeAllLabels:

self.removeAllLabels(completion: self.labelViews.removeAll())

Let's split this up:

let arg = self.labelViews.removeAll()
self. removeAllLabels(completion: arg)

What is the type of arg? We need it to be () -> Void. But if we option-click it in Xcode, we'll see that arg has type () (which is the same as Void). In fact, Swift emits a warning for it:

Constant 'arg' inferred to have type '()', which may be unexpected

What you probably meant to do is wrap the removeAll call in a closure, like this:

let arg = { self.labelViews.removeAll() }
self.removeAllLabels(completion: arg)

And once you have eliminated the error, you can put it back together in a single statement:

self.removeAllLabels(completion: { self.labelViews.removeAll() })
rob mayoff
  • 375,296
  • 67
  • 796
  • 848