function with dataTask returning a value

前端 未结 4 1215
深忆病人
深忆病人 2020-12-06 14:26

I wan\'t to check if my url statusCode equals to 200, I created a function returning a Boolean if the statusCode equals to 200, I\'m using a dataTask, but I don\'t know how

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 14:41

    You're returning a value from a Void function that is the completionHandler closure of dataTask(_:, _:)

    Regarding your code, there is something wrong: you can't return that value because it's executed on a different thread, it's an asynchronous operation. Please take a look at this thread: Returning data from async call in Swift function

提交回复
热议问题