Swift URL 中的引号
Swift Quotation Mark in URL
我正在尝试使用 Alamofire 对此进行请求 url:
https://overpass-api.de/api/interpreter?data=[out:json];(way[highway~"^(motorway)$"][!maxspeed](around:5,45.792790,3.062686););out%20tags;
但它包含双引号并且转换为 URL 失败。
我用反斜杠转义了 "
let urlString = "https://overpass-api.de/api/interpreter?data=[out:json];(way[highway~\"^(motorway)$\"][!maxspeed](around:5,45.792790,3.062686););out%20tags;"
但是当我在 URL 中转换字符串时,它 returns nil
let url = URL(string: urlString)
我已经尝试用 %22
替换 "
符号,但它仍然不起作用。我尝试使用 addingPercent 但它导致错误 url 和 returns 错误 400 或 404
let urlWithEconding = urlString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
我也尝试使用 AlamoFire 方法进行 url 转换和编码,但我无法使其工作...
以下是使用 URLComponents 的方法
let queryValue = "[out:json];(way[highway~\"^(motorway)$\"][!maxspeed](around:5,45.792790,3.062686););out tags;"
var components = URLComponents()
components.scheme = "https"
components.host = "overpass-api.de"
components.path = "/api/interpreter"
components.queryItems = [URLQueryItem(name: "data", value: queryValue)]
如果你不想转义引号,你可以像这样定义变量
let queryValue = """
[out:json];(way[highway~"^(motorway)$"][!maxspeed](around:5,45.792790,3.062686););out tags;
"""
我正在尝试使用 Alamofire 对此进行请求 url:
https://overpass-api.de/api/interpreter?data=[out:json];(way[highway~"^(motorway)$"][!maxspeed](around:5,45.792790,3.062686););out%20tags;
但它包含双引号并且转换为 URL 失败。
我用反斜杠转义了 "
let urlString = "https://overpass-api.de/api/interpreter?data=[out:json];(way[highway~\"^(motorway)$\"][!maxspeed](around:5,45.792790,3.062686););out%20tags;"
但是当我在 URL 中转换字符串时,它 returns nil
let url = URL(string: urlString)
我已经尝试用 %22
替换 "
符号,但它仍然不起作用。我尝试使用 addingPercent 但它导致错误 url 和 returns 错误 400 或 404
let urlWithEconding = urlString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
我也尝试使用 AlamoFire 方法进行 url 转换和编码,但我无法使其工作...
以下是使用 URLComponents 的方法
let queryValue = "[out:json];(way[highway~\"^(motorway)$\"][!maxspeed](around:5,45.792790,3.062686););out tags;"
var components = URLComponents()
components.scheme = "https"
components.host = "overpass-api.de"
components.path = "/api/interpreter"
components.queryItems = [URLQueryItem(name: "data", value: queryValue)]
如果你不想转义引号,你可以像这样定义变量
let queryValue = """
[out:json];(way[highway~"^(motorway)$"][!maxspeed](around:5,45.792790,3.062686););out tags;
"""