簡體   English   中英

無法將類型“[()]”的返回表達式轉換為返回類型

[英]Cannot convert return expression of type '[()]' to return type

我正在使用 map 來更新所有數組值。 但它返回一個空數組。

var filterProductList = [CategoryProduct]()
let finalList = filterProductList.map{ p in
            if let pf = test.rankings[0].products.first(where: {$0.id == p.id}) {
                p.rankCount = pf.viewCount
            }
        }
return finalList // Showing error here cannot convert return expression of type '[()]' to return type '[CategoryProduct]'

您必須顯式返回p並指定返回類型

let finalList = filterProductList.map{ p -> CategoryProduct in
     if let pf = test.rankings[0].products.first(where: {$0.id == p.id}) {
         p.rankCount = pf.viewCount
     }
     return p
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM