Cannot convert value of type 'any Error'

I have an async function where im using the withUnsafeContinuation method and closure but for some reason Xcode is not letting me pass either 'error' or 'Error' for continuation.resume(throwing:). I get the error "Cannot convert value of type 'any Error'"

  public func parseResponseData(data: Data) async throws -> TextResult {
            return try await withUnsafeContinuation { continuation in
                Task {
                    do {
                        let decoder = JSONDecoder()
                        let result = try await decoder.decode(TextResult.self, from: data)
                        continuation.resume(returning: result)
                    } catch {
                        continuation.resume(throwing: error)
                    }
                }
            }
        }
        

Replies

There’s two parts to this:

  • The specific error you’re hitting.

  • What this code is trying to do.

I’m going to tackle the second one first, because I think that’ll obviate the first one.

What are you trying achieve with this code? It seems redundant, because:

  1. You start off in a Swift async function.

  2. Then you create a continuation. This is usually a way to bridge from Swift concurrency to other forms of concurrency but…

  3. You then start a Swift concurrency task.

  4. Which calls a synchronous function.

  5. And finally completes your continuation.

This is weird because you’re going out and then back in to Swift concurrency. You could simplify this a lot by calling the JSON decoder directly in parseResponseData(data:).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"