我有一个项目数组
struct Item {
var id: String
}
如何使用 reduce 函数将所有 ID 附加到数组?
我的尝试:
self.items.reduce([String](), { $0.0.append($0.1.id)})
但是编译器显示错误:
Contextual closure type '(_, [Item]) -> _' expects 2 arguments, but 1 was used in closure body
最佳答案
试试这个:
items.reduce([String](), { res, item in
var arr = res
arr.append(item.id)
return arr
})
关于swift - 使用 reduce 将结构的属性附加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45463941/