swift - 在 Swift 5 中调用函数导致错误“表达式类型不明确,没有更多上下文”
问题描述
当调用我的函数从 API 获取一些位置时,我收到错误“表达式类型不明确,没有更多上下文”。
由于我对 iOS 开发和 swift 比较陌生,所以我不得不问你们。希望这只是我监督的一些简单的事情。我已经在网上搜索过,但没有任何帮助。
先感谢您!
这是我调用该函数的方式:
fetchLocation(latitude: latitude, longitude: longitude, locationsCompletionHandler: {
shopSites in
ForEach (shopSites){ }
})
这是功能:
func fetchLocation(latitude: Double, longitude: Double, locationsCompletionHandler: @escaping ([ShopSite]) -> Void) {
let semaphore = DispatchSemaphore (value: 0)
let domain = "http://e2cadda8d178.ngrok.io/api/v1/"
let urlString = domain + "public/location/" + String(latitude) + "/" + String(longitude) + "/50"
//let url = URL(string: "e2cadda8d178.ngrok.io/api/v1/public/location/66.68994/10.249066/50")!
let url = URL(string: urlString)!
var request = URLRequest(url: url,timeoutInterval: Double.infinity)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { json, response, error in
guard let json = json else {
print(String(describing: error))
semaphore.signal()
locationsCompletionHandler([])
return
}
let str = String(decoding: json, as: UTF8.self)
print(str)
let shopSites: [ShopSite] = try! JSONDecoder().decode([ShopSite].self, from: json)
print(shopSites.count)
semaphore.signal()
locationsCompletionHandler(shopSites)
}
task.resume()
semaphore.wait()
如果您需要更多详细信息,请立即告诉我。
解决方案
我认为您的 for each 循环代码可能有问题,请尝试将您的回调代码更改为
fetchLocation(latitude: latitude, longitude: longitude, locationsCompletionHandler: {
shopSites in
for site in shopSites {
}
})
推荐阅读
- android - Android在哪里存储安装程序名称?
- python - 如何构建一个项目以在 Pycharm 中发布它?
- c++ - LEA 将 RAX 设置为错误的函数地址
- javascript - 材质 ui 组件未显示在网格的底部
- c# - C# Collatz - 有人知道修复方法吗?
- c++ - mpic++ 未定义对 tbb::task 的 typeinfo 的引用
- python - 如何使用 django 通道管理 cpu 和内存
- maven - Intellij:致命错误编译:无效标志:--release [帮助 1]
- string - 接受数字或字符
- npm - npm :术语“npm”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称