Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使Http get请求URL由swift 3中的管道组成?_Ios_Swift_Http Get - Fatal编程技术网

Ios 如何使Http get请求URL由swift 3中的管道组成?

Ios 如何使Http get请求URL由swift 3中的管道组成?,ios,swift,http-get,Ios,Swift,Http Get,我收到了swift 3的get请求。该url具有管道。如果没有管道,它可以正常工作,但是当我添加管道时,代码会在展开可选值错误时中断,说find nil 这是我的网址 让url=url(字符串:“”) 这是我的密码: func synchronusGetRequstForExternalAPI(api_url:String, headers:[ String: String]) -> ResultModel { let resultModel = ResultModel(

我收到了swift 3的get请求。该url具有管道。如果没有管道,它可以正常工作,但是当我添加管道时,代码会在展开可选值错误时中断,说find nil

这是我的网址 让url=url(字符串:“”)

这是我的密码:

func synchronusGetRequstForExternalAPI(api_url:String, headers:[ String: String]) -> ResultModel {

        let resultModel = ResultModel()

        //create the url with URL
        let url = URL(string:"https://maps.googleapis.com/maps/api/directions/json?origin=colombo&destination=kandy&waypoints=optimize:true|&key=AIzaSyCNBcQLIVvNwqjkYiLawnYK_OK4EQbRM5M")
        //create the URLRequest object using the url object
        var request = URLRequest(url: url!)

        //set headers
        for item in headers {
            request.addValue(item.value, forHTTPHeaderField: item.key)
        }

        let semaphore = DispatchSemaphore(value: 0)
        let task = URLSession.shared.dataTask(with: request as URLRequest) {
            (data, response, error) in


            if(error != nil){
                resultModel.ErrorType = .NO_INT
                resultModel.JsonReslut = JSON.null

            }else{

                if let resp = response as? HTTPURLResponse{
                    if(resp.statusCode == 200){
                        if let jsonResult = JSON(data) as? JSON {

                            resultModel.ErrorType = .NO_ERROR
                            resultModel.JsonReslut = jsonResult
                        }
                    }else{
                        if let jsonResult = JSON(data) as? JSON {

                            resultModel.ErrorType = .SEREVR_ERROR
                            resultModel.JsonReslut = jsonResult
                        }else{
                            resultModel.ErrorType = .SEREVR_ERROR
                            resultModel.JsonReslut = JSON.null
                        }
                    }
                }
            }

            semaphore.signal()
        }
        task.resume()
        _ = semaphore.wait(timeout: DispatchTime.distantFuture)

        return resultModel
    }

有人能告诉我为什么会发生这种情况吗?

|
是URL中不允许的字符。尝试转义URL字符串

let url = URL(string:"https://maps.googleapis.com/maps/api/directions/json?origin=colombo&destination=kandy&waypoints=optimize:true|&key=AIzaSyCNBcQLIVvNwqjkYiLawnYK_OK4EQbRM5M".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet()))

|
不是URL中允许的字符。尝试转义URL字符串

let url = URL(string:"https://maps.googleapis.com/maps/api/directions/json?origin=colombo&destination=kandy&waypoints=optimize:true|&key=AIzaSyCNBcQLIVvNwqjkYiLawnYK_OK4EQbRM5M".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet()))