0

I am having a problem when I am trying to add async and await to a default function.

The function I am trying to add it to is:

func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String)

But it looks like when I add async the function will no longer get called when I press the button because it apparently is a different function. How can I fix this?

HangarRash
  • 7,314
  • 5
  • 5
  • 32
  • Call the function within an asynchronous task, using `await`. Look at the [documentation](https://developer.apple.com/documentation/swift/updating_an_app_to_use_swift_concurrency). – HunterLion May 05 '23 at 22:19
  • async/await is new api of Swift. You can write another function, mark it as async and put it in a `Task` inside the function you mentioned – Quang Hà May 05 '23 at 22:20
  • I see! I ended up going with lorem ipsums example but your suggestion sound good as well. Thanks for the tips! – Kristoffer Melen May 06 '23 at 22:38

1 Answers1

0

You can't change Apple's function but you can incorporate.

var task: Task<Void, Never>? = nil // Put this at class level.

func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String){

    task?.cancel() // Cancels previous operations
    task = Task{
         //Do something here
    }
}
    
lorem ipsum
  • 21,175
  • 5
  • 24
  • 48