Search code examples
iosarraysswiftenumerate

Iterating over multiple arrays in Swift


I'm looking to basically match up two different arrays in Swift using the enumerated method. So if I have:

let array1 = ["a", "b", "c", "d"]
let array2 = ["1", "2", "3", "4"]

I need to return a new array that would read:

newArray = ["1. a1", "2. b2", "3. c3", "4. d4"]

How do I make an array like that?


Solution

  • I managed to figure it out with some help from a friend:

    var newArray: [String] = []
        for (index, array1) in array1.enumerated() {
            newArray.append("\(index + 1). \(array1)(\(array2[index]))")
        }
        return newArray
    

    Thanks!