ios – Invalid conversion from ‘async’ function of type ‘(UIAlertAction)


The initialiser of UIAlertAction looks like this

convenience init(
    title: String?,
    style: UIAlertAction.Style,
    handler: ((UIAlertAction) -> Void)? = nil
)

The handler is an optional wrapping (UIAlertAction) -> Void but your closure that you pass to it has an asynchronous call in it, so its type is (UIAlertAction) async -> Void. This means it has the wrong type and cannot be used in place of a synchronous function.

You’ll need to wrap your await call in a Task e.g

Task
{
    await Library.sharedInstance.add(book: self.book, vc: self)
}

You’ll probably want to put the

self.removeFromLibrary = !self.removeFromLibrary

in the task too, or you might have a race condition.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img