小编Ron*_*Aja的帖子

使用HTTPURLResponse代替URLResponse

我想调用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)

http swift3

3
推荐指数
1
解决办法
1412
查看次数

类型“任何”不符合协议“序列”

我在代码中遇到麻烦,当尝试解析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)

arrays ios swift swift3

3
推荐指数
1
解决办法
8974
查看次数

标签 统计

swift3 ×2

arrays ×1

http ×1

ios ×1

swift ×1