array to string in swift

To convert an array of elements to a string in Swift, you can use the joined method on the array, which concatenates the elements of the array into a single string separated by a specified delimiter.

Here's an example:

main.swift
let array = ["apple", "banana", "cherry"]
let str = array.joined(separator: ", ")
print(str) // Output: "apple, banana, cherry"
128 chars
4 lines

In this example, array is an array of strings, and we use the joined method to concatenate the elements using ", " as the delimiter.

Note that the joined method returns a String object, so you can assign the result to a string variable (str in this example) for further use.

gistlibby LogSnag