gpt4 book ai didi

ios - 我怎样才能从 NSUrlSession 返回一个 NSHttpUrlResponse 状态码

转载 作者:行者123 更新时间:2023-11-28 11:10:46 29 4
gpt4 key购买 nike

<分区>

如何从 NSUrlSession 返回一个 NSHttpUrlResponse 状态码。我对这种语言很陌生。所以,如果有其他更好的做法,请告诉我。我的代码就在下面。

//Method to return a status code
func responseCode() -> Int{
var responseCode : Int=0
if Reachability.isConnectedToNetwork() == true {
// get news feed url
let url = NSURL(string: baseUrl)
let session = NSURLSession.sharedSession()
//create request
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = httpMethod
//check null value
if (bodyData != "") {
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
}
if (auth != "") {
print("Token Auth : \(auth)")
request.addValue("bearer \(self.auth)", forHTTPHeaderField: "Authorization")
}
print("request : \(request)")

//How to return a Integer from this below function
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) -> Void in
print("Error\(error)")
if let httpResponse = response as? NSHTTPURLResponse {
print("HttpReponse\(httpResponse.statusCode)")
// Get a status code and assign it in a variable
responseCode = httpResponse.statusCode
}
})
task.resume()
//Return a response code in ViewController but it just return a initial set value that is 0
return responseCode
}
//Return a responseCode in ViewController but it just return a initial set value that is 0
return responseCode
}

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com