0

I am for some reason getting this error from my code, yet I have specified the return type.

Unexpected non-void return value in void function

func performSearch(region: MKCoordinateRegion) -> MKLocalSearch.Response {
    print("Searching...")
    let searchRequest = MKLocalSearch.Request()
    searchRequest.naturalLanguageQuery = "Coffee"
    searchRequest.region = region
    let search = MKLocalSearch(request: searchRequest)
    search.start { response, error in
        guard let response = response else {
            print("Error: \(error?.localizedDescription ?? "Unknown error").")
            return
        }
        return response
    }
}
Cal
  • 422
  • 6
  • 20

1 Answers1

0

This is probably because the completion block that is passed to search.start shouldn't have a return value. Since the return statement is inside the completion block, it is treated as a return statement for the completion block, and not for performSearch.

Arik Segal
  • 2,963
  • 2
  • 17
  • 29
  • Is there a way to return it while still inside the search block? As otherwise the function will return a value before the search has finished – Cal Dec 04 '22 at 10:03
  • In newer versions of Swift you can use Async / Await, read here: https://www.avanderlee.com/swift/async-await/ – Arik Segal Dec 05 '22 at 09:46