How to return value from a Closure in Swift?

后端 未结 2 513
逝去的感伤
逝去的感伤 2021-02-07 12:09

So I am using fabric plugin/Twitter-kit to use twitter api in my application. I want to get the image URL of profile picture of a celebrity. here is my code below.



        
2条回答
  •  醉梦人生
    2021-02-07 12:53

    You are correct, sendTwitterRequest will return after your function has already returned, so there is no way for the outer function to return an imageURL.

    Instead, in the closure, take the return value that you want in the cell and store it somewhere (in a member variable) and have the tableView update it itself (e.g. with tableView.reloadData()).

    This will cause it to get the cell again (cellForRow ...). Change the implementation to use the member variable where you stored the value from the call.

提交回复
热议问题