How to convert an Array to a String in Swift?
Hi everyone! I just started learning Swift and ran into what seems like a simple task, but I can't figure it out. I have an array of strings:
```swift
let fruits = ["Apple", "Banana", "Orange", "Grape"]
```
I want to turn this array into a single string to display it in a UILabel or send it to a server. For example, I want to get something like: `"Apple, Banana, Orange, Grape"` or `"Apple Banana Orange Grape"`.
I tried using `String(fruits)`, but it gives a compilation error. I attempted to write a for-in loop and manually add elements to the string, but it looks terrible, and I don't know how to properly handle the last element without an extra comma.
Please tell me how to do this correctly and elegantly in Swift? It's important that the solution is performant and suitable for
No answers yet.
Log in to leave an answer