【问题标题】:How to assign the property of one array of objects to another array of strings?如何将一个对象数组的属性分配给另一个字符串数组?
【发布时间】:2020-08-20 10:37:50
【问题描述】:

我有一个项目数组,想知道是否有更简单的方法可以将每个属性item.fullPath 分配给另一个数组。现在我正在使用for in 循环。

var items: Array<String> = []
        for item in storageListResult.items {
            items.append(item.fullPath)
        }

但是有没有类似的东西:

let items = storageListResult.items[].fullPath

【问题讨论】:

    标签: ios arrays swift firebase-storage for-in-loop


    【解决方案1】:

    你可以使用地图来做到这一点

    let items = storageListResult.items.map{$0.fullPath}
    

    在 swift 5.2 中你也可以这样做

    let items = storageListResult.items.map(\.fullPath)
    

    【讨论】:

    • 我添加了.items 以使其成为let items = storageListResult.items.map{$0.fullPath},它完美运行!我建议编辑。谢谢!
    猜你喜欢
    • 2019-07-16
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2019-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多