Hamza
Hamza

Reputation: 151

Display two array data in label swift

i have two String arrays, each array will have same number of elements. Now i want to show arrays data in a label text.

For example, arr1["A","B","C"] and arr2["D","E","F"] we have these two arrays.

Now how we can show data in label like this? label.text = A:D,B:E,C:F. How i can show data in this format in my label?. Array1 element should be first and second should be Array2 element.

Upvotes: 0

Views: 369

Answers (1)

PGDev
PGDev

Reputation: 24341

Simply use zip(_:_:), map(_:) and joined(separator:) on the arr1 and arr2 to get the expected result, i.e.

let arr1 = ["A","B","C"]
let arr2 = ["D","E","F"]

let text = zip(arr1, arr2).map{ "\($0.0):\($0.1)" }.joined(separator: ",") //A:D,B:E,C:F
label.text = text

Upvotes: 5

Related Questions