Swift:使用贴图闭包转换自定义对象
我有一个自定义对象数组,我想将它们转换为其他自定义对象数组 我可以用地图闭包吗 我这样试过:Swift:使用贴图闭包转换自定义对象,swift,closures,Swift,Closures,我有一个自定义对象数组,我想将它们转换为其他自定义对象数组 我可以用地图闭包吗 我这样试过: items.map( { (cutom: Custom1) -> Custom2 in Custom2(title: custom.name, locationName: custom.placeName)) }) 但是没有结果…编译器给了我以下错误: _?? 不能转换为“数组” 谢谢你的回复 您可能是指custom:Custom1而不是c
items.map( { (cutom: Custom1) -> Custom2 in
Custom2(title: custom.name, locationName: custom.placeName))
})
但是没有结果…编译器给了我以下错误:
_?? 不能转换为“数组”
谢谢你的回复 您可能是指
custom:Custom1
而不是cutom
。在Custom2
initI中,您还获得了一个额外的“)”。我试图实现您的代码(我删除了额外的标记)。它起作用了。我回滚了编辑@pixyzehn请参见:在闭包中插入return语句或删除除Custom2(…)之外的所有内容,并使用$0而不是custom:Custom1