我想调用API及其应用程序的一部分,但我想确保获得要调用的站点的状态代码(以防止运行时错误)。我无法获取URLResponse的状态代码,但可以获取HTTPURLResponse的状态代码。如何使用后者代替前者?
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)
}
}
Run Code Online (Sandbox Code Playgroud) 我在代码中遇到麻烦,当尝试解析JSON数据(数组的每个数据,应如何完成)并尝试设置for in循环时,错误就会出现。这是我的代码
if let jsonDataArray = try? JSONSerialization.jsonObject(with: data!, options: [])
{
print(jsonDataArray)
var allStops = [busStops]()
for eachData in jsonDataArray
^
//this is where the error is located
{
if let jsonDataDictionary = eachData as? [String : AnyObject]
{
let eachStop = busStops(jsonDataDictiony: jsonDataDictionary)
}
}
}
Run Code Online (Sandbox Code Playgroud)