我在以下功能中有完成处理程序
static func fetchFeatureApp(completionHandler:@escaping ([AppCategory1])->()){
let urlString="http://ebmacs.net/ubereats/Api/all_product?id=1"
let url = URL(string: urlString)!
URLSession.shared.dataTask(with: URLRequest(url: url)){ (data, responce, error) in
if error != nil
{
print(error)
return
}
do {
if let jsonResult = try JSONSerialization.jsonObject(with:data!) as? [String:Any] {
if let products = jsonResult["products"] as? [[String:String]] {
var appCategories = [AppCategory1]()
for product in products {
let category = AppCategory1(id: product["product_id"] ?? "",
name: product["product_name"] ?? "",
description: product["product_description"] ?? "",
url: product["image_url"] ?? "",
price: product["product_price"] ?? "")
appCategories.append(category)
}
print("Abc=")
print(appCategories)
dispatch_async(dispatch_get_main_queue(), {
completionHandler(appCategories)
})
}
}
} catch {
print(error)
}
}.resume()
}
上述函数中的dispatch async给出了错误
dispatch_async(dispatch_get_main_queue(), {
completionHandler(appCategories)
})
错误陈述是
'dispatch_async'已被实例方法取代 'DispatchQueue.async(执行:)' 'dispatch_get_main_queue()'已被属性取代 'DispatchQueue.main'
如何更正此错误?
我调用此函数
AppCategory.fetchFeatureApp { (appcategories) in
self.appcategories = appcategories
self.collectionView?.reloadData()
}
给出错误
'FeaturedViewController'类型的值没有成员'appcategories'
如何纠正函数调用?
答案 0 :(得分:2)
这是现代(Swift 3及更高版本)语法:
DispatchQueue.main.async { completionHandler(appCategories) }