How to add text prefix to an array on Swift?

Right now I have an array that when printing just shows what number I sent. I would like the word to "car"be in front of each array number. For example: I enter 1 and 2 into an array. When an array is called, it will look like [car 1, car 2]not [1,2].

I added my array variable and what I call to print the array:

var arrayOfInt = [Int]()
label.text = String(describing: arrayOfInt)
+4
source share
1 answer

Try the following:

let arrayOfInt: [Int] = [1, 2]
let cars = arrayOfInt.map { "car \($0)" }

as a result, the array carswill be:

["car 1", "car 2"]

finally, convert to a string as before:

label.text = String(describing: cars)

Array.map , . , , .

+7

Source: https://habr.com/ru/post/1676574/


All Articles