[英]Confusing Swift error: Type of expression is ambiguous without more context
I'm getting this confusing error: Type of expression is ambiguous without more context
in my function below and I have no clue what is causing it.我收到了这个令人困惑的错误:在下面的 function 中Type of expression is ambiguous without more context
,我不知道是什么原因造成的。 I think it has something to do with my three let
constants, because when I made some changes there the error appeared.我认为这与我的三个let
常量有关,因为当我在那里进行一些更改时,出现了错误。
private func FetchOriginCoordinates(completion: @escaping ([NominationStructure]) -> ()) {
let locationUrl = "https://nominatim.openstreetmap.org/search?country=Sweden&city=Stockholm&street=\(self.origin.input)&format=json"
let encodedLink = locationUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let encodedURL = URL(string: encodedLink!)
URLSession.shared.dataTask(with: locationUrl!) {data, response, error in
if let data = data {
do {
if let decodedJson = try? JSONDecoder().decode([NominationStructure].self, from: data) {
DispatchQueue.main.async {
completion (decodedJson)
}
}
} catch {
print(error)
}
}
}.resume() // This is where the error appears
}
The error in Swift most of the times is not really helpful. Swift 大多数时候的错误并没有真正的帮助。 Your issue is that you are trying to pass a string instead of an URL to your data task method.您的问题是您试图将字符串而不是 URL 传递给您的数据任务方法。 Besides that you should always handle the error instead of ignoring it.除此之外,您应该始终处理错误而不是忽略它。 I would also change the method declaration to include the error in your completion handler:我还将更改方法声明以在您的完成处理程序中包含错误:
Note: It is Swift naming convention to name your methods starting with a lowercase letter.注意:Swift 命名约定以小写字母开头命名您的方法。
private func fetchOriginCoordinates(completion: @escaping ([Place]?, Error?) -> ()) {
let url = URL(string: "your APY string")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else {
completion(nil, error)
return
}
do {
let decodedJson = try JSONDecoder().decode([Place].self, from: data)
DispatchQueue.main.async {
completion(decodedJson, nil)
}
} catch {
completion(nil, error)
}
}.resume()
}
edit/update:编辑/更新:
To properly encode your URL you should use URLComponents and add the address components to its query items:要正确编码您的 URL 您应该使用 URLComponents 并将地址组件添加到其查询项中:
private func fetchOriginCoordinates(for street: String,
country: String = "Sweden",
city: String = "Stockholm",
format: String = "json",
completion: @escaping ([Place]?, Error?) -> ()) {
var urlComp = URLComponents()
urlComp.scheme = "https"
urlComp.host = "nominatim.openstreetmap.org"
urlComp.path = "/search"
urlComp.queryItems = [
.init(name: "country", value: country),
.init(name: "city", value: city),
.init(name: "street", value: street),
.init(name: "format", value: format)
]
guard let url = urlComp.url else {
completion(nil, URL.Error.invalidCharacterFound(street))
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else {
completion(nil, error)
return
}
do {
let decodedJson = try
JSONDecoder().decode([Place].self, from: data)
DispatchQueue.main.async {
completion(decodedJson, nil)
}
} catch {
completion(nil, error)
}
}.resume()
}
Usage:用法:
struct Place: Codable {
let placeID: Int
let licence: String
let osmType: OsmType
let osmID: Int
let boundingbox: [String]
let lat, lon, displayName: String
let rootClass: Class
let type: TypeEnum
let importance: Double
enum CodingKeys: String, CodingKey {
case placeID = "place_id", licence, osmType = "osm_type", osmID = "osm_id", boundingbox, lat, lon, displayName = "display_name", rootClass = "class", type, importance
}
}
enum OsmType: String, Codable {
case way
}
enum Class: String, Codable {
case highway
}
enum TypeEnum: String, Codable {
case residential, secondary, unclassified
}
extension URL {
enum Error: Swift.Error {
case invalidCharacterFound(String)
}
}
fetchOriginCoordinates(for: "Valhallavägen") { results, error in
guard let results = results else {
print("error:", error ?? "nil")
return
}
for result in results {
print(result)
}
}
This will print:这将打印:
Place(placeID: 100830896, licence: "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 41510350, boundingbox: ["59.3431062", "59.3438379", "18.0792506", "18.0821732"], lat: "59.3434756", lon: "18.0807072", displayName: "Valhallavägen, Ladugårdsgärdet, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 114 36, Sweden", rootClass: Class.highway, type: TypeEnum.secondary, importance: 0.6138276880810758) Place(placeID:100830896,许可证:“数据© OpenStreetMap 贡献者,ODbL 1.0。https://osm.org/copyright” ,osmType:OsmType.way,osmID:41510350,边界框:[“59.343837.94”, 18.0792506", "18.0821732"], lat: "59.3434756", lon: "18.0807072", displayName: "Valhallavägen, Ladugårdsgärdet, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 114 36, Sweden", rootClass: Class.highway,类型:TypeEnum.secondary,重要性:0.6138276880810758)
Place(placeID: 110921085, licence: "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 95305155, boundingbox: ["59.3473458", "59.3475855", "18.0653022", "18.0654864"], lat: "59.3475219", lon: "18.0653483", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 114 24, Sweden", rootClass: Class.highway, type: TypeEnum.secondary, importance: 0.6138276880810758) Place(placeID:110921085,许可证:“数据© OpenStreetMap 贡献者,ODbL 1.0。https://osm.org/copyright” ,osmType:OsmType.way,osmID:95305155,boundingbox:[“59.34735555.3”, 18.0653022", "18.0654864"], lat: "59.3475219", lon: "18.0653483", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 114 24, Sweden", rootClass: Class .highway,类型:TypeEnum.secondary,重要性:0.6138276880810758)
Place(placeID: 98613676, licence: "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 33057170, boundingbox: ["59.3458032", "59.3459006", "18.0692504", "18.0695413"], lat: "59.3459006", lon: "18.0692504", displayName: "Valhallavägen, Lärkstaden, Östermalm, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 100 41, Sweden", rootClass: Class.highway, type: TypeEnum.secondary, importance: 0.6138276880810758) Place(placeID:98613676,许可证:“数据©OpenStreetMap贡献者,ODbL 1.0。https://osm.org/copyright” ,osmType:OsmType.way,osmID:33057170,边界框:[“59.3458032”,“59.3458036”,“ 18.0692504", "18.0695413"], lat: "59.3459006", lon: "18.0692504", displayName: "Valhallavägen, Lärkstaden, Östermalm, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 100 41, Sweden", rootClass: Class.高速公路,类型:TypeEnum.secondary,重要性:0.6138276880810758)
Place(placeID: 97771827, licence: "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 32125128, boundingbox: ["59.3435989", "59.3440214", "18.0767179", "18.0788456"], lat: "59.3437642", lon: "18.0781052", displayName: "Valhallavägen, Villastaden, Östermalm, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 114 36, Sweden", rootClass: Class.highway, type: TypeEnum.secondary, importance: 0.6138276880810758) Place(placeID: 217216781, licence: "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 621715886, boundingbox: ["59.3400363", "59.3400593", "18.0943109", "18.0944135"], lat: "59.3400593", lon: "18.0943109", displayName: "Valhallavägen, Ladugårdsgärdet, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 115 24, Sweden", rootClass: Z9BD81329FEBF6EFE Place(placeID:97771827,许可证:“数据© OpenStreetMap 贡献者,ODbL 1.0。https://osm.org/copyright” ,osmType:OsmType.way,osmID:32125128,边界框:[“59.343592184”,“59.343592184”, 18.0767179", "18.0788456"], lat: "59.3437642", lon: "18.0781052", displayName: "Valhallavägen, Villastaden, Östermalm, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 114 36, Sweden", rootClass: Class.高速公路,类型:TypeEnum.secondary,重要性:0.6138276880810758)地点(placeID:217216781,许可证:“数据 © OpenStreetMap 贡献者,ODbL 1.0。https://osm.org/copyright” ,osmType:21715,osmType:8,6,OsmType:8。 boundingbox: ["59.3400363", "59.3400593", "18.0943109", "18.0944135"], lat: "59.3400593", lon: "18.0943109", displayName: "Valhalllavägen, Ladugårdsgärdet, Österholmms County, Stockholm stadsdelsområ, Stockholmåms 115 24,瑞典”,根类:Z9BD81329FEBF6EFE 22788E03DDEAF0AFZ.highway, type: TypeEnum.secondary, importance: 0.6138276880810758) Place(placeID: 91379768, licence: "Data © OpenStreetMap contributors, ODbL 1.0. 22788E03DDEAF0AFZ.highway,类型:TypeEnum.secondary,重要性:0.6138276880810758)地点(地点ID:91379768,许可证:“数据©OpenStreetMap贡献者,ODbL 1.0。 https://osm.org/copyright" , osmType: OsmType.way, osmID: 16620413, boundingbox: ["59.3500575", "59.3502804", "18.0588563", "18.0593429"], lat: "59.3500575", lon: "18.0593429", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 113 54, Sweden", rootClass: Class.highway, type: TypeEnum.secondary, importance: 0.6138276880810758) https://osm.org/copyright" , osmType: OsmType.way, osmID: 16620413, boundingbox: ["59.3500575", "59.3502804", "18.0588563", "18.0588563", "18.059343505", "59343507", 18.0593429", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 113 54, Sweden", rootClass: Class.highway, 类型: Type: Type: Type: Type: Type:
Place(placeID: 122081536, licence: "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 137883176, boundingbox: ["59.3467668", "59.3469813", "18.0671955", "18.0678302"], lat: "59.3468409", lon: "18.0676219", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 100 41, Sweden", rootClass: Class.highway, type: TypeEnum.residential, importance: 0.6138276880810758) Place(placeID: 210614450, licence: "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 571062827, boundingbox: ["59.3469813", "59.3471276", "18.067152", "18.0671955"], lat: "59.3469813", lon: "18.0671955", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 114 24, Sweden", rootClas Place(placeID: 122081536, licence: "数据 © OpenStreetMap 贡献者, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 137883176, boundingbox: [, "59.346697613." 18.0671955", "18.0678302"], lat: "59.3468409", lon: "18.0676219", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 100 41, Sweden", rootClass: Class .highway,类型:TypeEnum.residential,重要性:0.6138276880810758)地点(placeID:210614450,许可证:“数据© OpenStreetMap 贡献者,ODbL 1.0。https://osm.org/copyright62” ,27ID:5710OsmType , boundingbox: ["59.3469813", "59.3471276", "18.067152", "18.0671955"], lat: "59.3469813", lon: "18.0671955", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms, Stockholms stadholmskommunde, Stockholms , 斯德哥尔摩县, 114 24, 瑞典", rootClas s: Class.highway, type: TypeEnum.residential, importance: 0.6138276880810758) s:Class.highway,类型:TypeEnum.residential,重要性:0.6138276880810758)
Place(placeID: 100832986, licence: "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 40886504, boundingbox: ["59.3459069", "59.3467668", "18.0678302", "18.0702113"], lat: "59.3464817", lon: "18.0686787", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 100 41, Sweden", rootClass: Class.highway, type: TypeEnum.unclassified, importance: 0.6138276880810758) Place(placeID: 100832986, licence: "数据 © OpenStreetMap 贡献者, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 40886504, boundingbox: ["59.345766.8, "59.3457066.8, 18.0678302", "18.0702113"], lat: "59.3464817", lon: "18.0686787", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 100 41, Sweden", rootClass: Class .highway,类型:TypeEnum.unclassified,重要性:0.6138276880810758)
Place(placeID: 172222242, licence: "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 345280785, boundingbox: ["59.3453872", "59.3458192", "18.0701008", "18.0715971"], lat: "59.3457283", lon: "18.0702244", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 114 26, Sweden", rootClass: Class.highway, type: TypeEnum.unclassified, importance: 0.6138276880810758) Place(placeID: 172222242, licence: "数据 © OpenStreetMap 贡献者, ODbL 1.0. https://osm.org/copyright" , osmType: OsmType.way, osmID: 345280785, boundingbox: [, "59.34583872" 18.0701008", "18.0715971"], lat: "59.3457283", lon: "18.0702244", displayName: "Valhallavägen, Ruddammen, Norra Djurgården, Östermalms stadsdelsområde, Stockholm, Stockholms kommun, Stockholm County, 114 26, Sweden", rootClass: Class .highway,类型:TypeEnum.unclassified,重要性:0.6138276880810758)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.