Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

iOS SwiftUI – Cannot pass function of type '() async -> Void' to parameter expecting synchronous function type

                        TextField("search", text: $searchText)
                            .accentColor(Color("ClickableLink"))
                            .foregroundColor(.white)
                            .focused($focusedField, equals: .field)
                            .onAppear {
                                self.focusedField = .field
                            }
                            .onSubmit {
                                loadinglayout = true
                                
                                do {
                                    try await getMemes()
                                }
                                catch {
                                    print(error)
                                }
                            }

I get

"Cannot pass function of type ‘() async -> Void’ to parameter expecting synchronous function type"

inside onSubmit.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So far the

do 

method solved this kind of issues, why not here? I have absolutely no idea what else I could try

>Solution :

The problem is that you are trying to call an async method from a synchronous method. onSubmit is synchronous, while getMemes is async.

You need to wrap async methods in a Task if you want to call them from a synchronous context.

.onSubmit {
    loadinglayout = true
    Task {
        do {
            try await getMemes()
        }
        catch {
            print(error)
        }
    }
}                            
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading