list a array in swift

To list an array in Swift, you can simply use a for loop to iterate through the elements in the array and print them out. Here's an example:

main.swift
let myArray = ["apple", "banana", "orange"]

for element in myArray {
    print(element)
}
91 chars
6 lines

This will output:

main.swift
apple
banana
orange
20 chars
4 lines

Alternatively, you can use the joined method to concatenate the elements of the array into a single string, separated by a delimiter of your choice:

main.swift
let myArray = ["apple", "banana", "orange"]
let delimiter = ", "

let concatenatedString = myArray.joined(separator: delimiter)
print(concatenatedString)
154 chars
6 lines

This will output:

main.swift
apple, banana, orange
22 chars
2 lines

related categories

gistlibby LogSnag