0
我想調用一個API及其應用程序的一部分,但我想確保我得到我要調用的網站的狀態代碼(以防止運行時錯誤)。我無法獲得URLResponse的狀態碼,但我可以通過HTTPURLResponse獲取它。我如何用後者代替前者?使用HTTPURLResponse代替URLResponse
let requestURLString = "my_http_url"
let requestURL = URL(string: requestURLString)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let ETAreq = session.dataTask(with: requestURL!)
{
(data, response (this is the URLResponse), error) in
if error == nil
{
//switch (this is where I will put switch of status code)
}
}
非常感謝它的實際工作! –