func fetch(completionHandler: @escaping ([OnSaleItem], [SlideItem], [NewArrivalItem], [EditorChoiceItem], Error?) -> Void) {
apiCaller.fetchUser(completionHandler: { baseData, error in
if let baseData = baseData {
self.apiCaller.fetchSlideOnSaleEditorChoiceNewArrivalData(baseData: baseData, completionHandler: { [self] decodedString, error in
if let decodedString = decodedString {
let data = convertToDictionary(text: decodedString)
let onSaleProductResponse = data["onsale"] as! NSArray
let slideResponse = data["slide"] as! NSArray
let newArrivalResponse = data["newarrival"] as! NSArray
let editorChoiceResponse = data["featured"] as! NSArray
var onSaleProducts : [Any] = []
var slide : [Any] = []
var newArrivalProducts : [Any] = []
var editorChoiceProducts : [Any] = []
for item in onSaleProductResponse {
onSaleProducts.append(item as! Dictionary<String,String>)
}
for data in onSaleProducts {
onSaleItems.append(OnSaleItem(dict: data as! [String : String]))
}
for item in slideResponse {
slide.append(item as! Dictionary<String,String>)
}
for data in slide {
slideItems.append(SlideItem(dict: data as! [String : String]))
}
for item in newArrivalResponse {
newArrivalProducts.append(item as! Dictionary<String,String>)
}
for data in newArrivalProducts {
newArrivalItems.append(NewArrivalItem(dict: data as! [String : String]))
}
for item in editorChoiceResponse {
editorChoiceProducts.append(item as! Dictionary<String,String>)
}
for data in editorChoiceProducts {
editorChoiceItems.append(EditorChoiceItem(dict: data as! [String : String]))
}
completionHandler(onSaleItems, slideItems, newArrivalItems, editorChoiceItems, nil)
}
})
self.apiCaller.fetchProducts(baseData: baseData, completionHandler: { [self] dataProducts, error in
if let dataProducts = dataProducts {
let data = convertToDictionary(text: dataProducts)
let productResponse = data["data"] as! NSArray
var products : [Any] = []
for item in productResponse {
products.append(item as! Dictionary<String,String>)
}
for data in products {
daftarProdukItems.append(DaftarProdukItem(dict: data as! [String : String]))
}
//this line is the problem
completionHandler(daftarProdukItems, nil)
}
})
}
})
}
当我需要使用完成处理程序返回但我从 2 个 API 端点请求时,我遇到了问题。
第一个端点是 fetchSlideOnSale...
第二个端点是 fetchProducts
附加信息:当我需要获取第一个或第二个端点时,我首先需要 baseData
我无法像下面的代码那样返回完成处理程序。行代码问题返回此错误
无法将类型“[DaftarProdukItem]”的值转换为预期参数类型“[OnSaleItem]”
检查下面的代码以获取 DispatchGroup 解决方案。