首页 > 解决方案 > 无法将类型“[()]”的返回表达式转换为返回类型

问题描述

我正在使用地图来更新所有数组值。但它返回一个空数组。

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]'

标签: iosarraysswiftdata-structures

解决方案


您必须显式返回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
}

推荐阅读