Swift NSURLConnection sendSynchronousRequest

前端 未结 1 2208
粉色の甜心
粉色の甜心 2021-02-19 15:18

In Objective C the following syntax was possible:

NSHTTPURLResponse *response = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returni         


        
相关标签:
1条回答
  • 2021-02-19 15:52

    Instead of manually creating an AutoreleasingUnsafeMutablePointer, you can simply use & to wrap the value you want to send a pointer to - your code becomes much simpler:

    var response: NSURLResponse?
    var error: NSError?
    let urlData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)
    

    After the call, you need to cast your response to NSHTTPURLResponse to access those properties:

    if let httpResponse = response as? NSHTTPURLResponse {
        println(httpResponse.statusCode)
    }
    
    0 讨论(0)
提交回复
热议问题