Search code examples
iosarraysswiftfirebase-storagefor-in-loop

How to assign the property of one array of objects to another array of strings?


I got an array of items and would like to know if there is an easier way to assign each property item.fullPath to another array. Right now I am using a for in loop.

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

But is there something similar to this:

let items = storageListResult.items[].fullPath

Solution

  • You can do it using map

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

    And in swift 5.2 you can also do it like

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