Swift – Array as String

To get array as a String in Swift, use description property of this Array instance. Array.description property returns a String that is textual representation of this array and its elements.

The following is a quick code snippet to get Array array as string.

array.description

Examples

In the following program, we take an array with three elements, and get its string representation using Array.description property.

main.swift

var arr = ["apple", "banana", "cherry"]
print(arr.description)

Output

["apple", "banana", "cherry"]
Program ended with exit code: 0

In the following program, we take an array with Any type of elements, and get its string representation using Array.description property.

main.swift

var arr: [Any] = [2, 4, 6, "apple", "banana"]
print(arr.description)

Output

[2, 4, 6, "apple", "banana"]
Program ended with exit code: 0
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we learned how to get the index of a specific element in given Array in Swift programming.