format String with array of Strings

String(format:, arguments: ) expects a [CVarArgType] as the second parameter:

let format = "%@ and %@!"
let str1 = "bla"
let str2 = "blob"

private func formatItNicely(format: String, substrings: [CVarArgType]) -> String {
    return String(format: format, arguments: substrings)
}

let result = formatItNicely(format, substrings: [str1, str2])
print(result) // bla and blob!

But note that this can crash (or give unexpected output) if the format specifiers do not match the actual arguments, the formatting functions do not (cannot) check that.

Tags:

Arrays

Ios

Swift