Search code examples
iosswiftswift2ios9

Append an array of strings to a string in Swift


I have an array of strings for one variable, and a string as another variable. I'd like to append all of the strings in the collection to the single string.

So for example I have:

 var s = String()

   //have the CSV writer create all the columns needed as an array of strings
   let arrayOfStrings: [String] = csvReport.map{GenerateRow($0)}

// now that we have all the strings, append each one 
        arrayOfStrings.map(s.stringByAppendingString({$0}))

the line above fails. I've tried every combination I can think of, but at the end of the day, I can't get it unless I just create a for loop to iterate through the entire collection, arrayOfStrings, and add it one by one. I feel like I can achieve this the same way using map or some other function.

Any help?

Thanks!


Solution

  • You can use joined(separator:):

    let stringArray = ["Hello", "World"]
    let sentence = stringArray.joined(separator: " ")  // "Hello World"