Converting Swift Array to String: A Complete Guide for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Convert Swift Array to String? Full Tutorial with Examples

\n\n

Hello, future iOS developer! If you've ever wondered "How do I convert a Swift Array to a String?" — you're not alone. This question has gathered thousands of views on Stack Overflow, and today we'll settle it once and for all. Why is this needed? Imagine: you're collecting data from a form, logging errors, or preparing text to send to a server. In all these cases, you need to turn an array into a readable string. Let's go!

\n\n

The Simplest Way: The joined() Method

\n\n

In Swift, to combine an array of strings into a single string, use the joined() method. This is the shortest path from an array to a string. Take a look at the basic example:

\n\n
let fruits = ["Apple", "Banana", "Orange"]\nlet fruitsString = fruits.joined()\nprint(fruitsString) // Output: "AppleBananaOrange"
\n\n

However, the result looks clumped together. To make the string readable, use joined(separator:) — it adds a separator between elements:

\n\n
let fruits = ["Apple", "Banana", "Orange"]\nlet fruitsString = fruits.joined(separator: ", ")\nprint(fruitsString) // Output: "Apple, Banana, Orange"
\n\n

Important: joined() works only with arrays containing strings ([String]). If you have an array of numbers or other types, you first need to convert each element.

\n\n

The Universal Method: map() + joined()

\n\n

What do you do if you have an array of integers ([Int]), floating-point numbers ([Double]), or, say, custom structures? The map() method comes to the rescue. This method transforms each element according to a given rule, and then we combine the result:

\n\n
let numbers = [1, 2, 3, 4, 5]\nlet numbersString = numbers.map { String($0) }.joined(separator: ", ")\nprint(numbersString) // Output: "1, 2, 3, 4, 5"
\n\n

Here, map { String($0) } turns each number into a string. After that, the array of strings is easily combined using joined().

\n\n

Example with Complex Objects

\n\n

Let's say you have an array of users, and you need to get a string with their names:

\n\n
struct User {\n    let name: String\n    let age: Int\n}\n\nlet users: [User] = [\n    User(name: "Anna", age: 25),\n    User(name: "Boris", age: 30),\n    User(name: "Victoria", age: 28)\n]\n\nlet namesString = users.map { $0.name }.joined(separator: ", ")\nprint(namesString) // Output: "Anna, Boris, Victoria"
\n\n

The Advanced Way: reduce(into:)

\n\n

The reduce(into:) method is a more flexible tool. It allows you to build a string with full control over the process. For example, you can add a prefix, suffix, or number the elements:

\n\n
let colors = ["Red", "Green", "Blue"]\nlet result = colors.reduce(into: "My colors: ") { (partialResult, color) in\n    partialResult += color + ", "\n}\n// Remove the last ", " if needed\nlet finalString = String(result.dropLast(2))\nprint(finalString) // Output: "My colors: Red, Green, Blue"
\n\n

Note: we manually remove the last two characters (comma and space). This makes the code less elegant than joined(), but reduce is indispensable when complex assembly logic is needed.

\n\n

Handling Optional Values (Optionals)

\n\n

In real projects, arrays often contain nil. If you simply call joined(), the compiler will throw an error. The solution is to filter out nil using compactMap():

\n\n
let optionalStrings: [String?] = ["Hello", nil, "World", nil, "Swift"]\nlet safeString = optionalStrings.compactMap { $0 }.joined(separator: " ")\nprint(safeString) // Output: "Hello World Swift"
\n\n

compactMap { $0 } automatically discards all nil values and returns

Blogs

Book Recommendations