0

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.

3
  • 1
    Does this answer your question? How to merge two arrays in Swift Commented Nov 21, 2019 at 11:18
  • Your link was also good to get the main idea. @JoakimDanielson Commented Nov 21, 2019 at 11:32
  • sure brother. @JoakimDanielson Commented Nov 21, 2019 at 11:53

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

5 Comments

Yes this is the right one as i want, thanks alot. Can i know what this map do? . @PGDev
map(_:) transforms every element into the required result. Read more about it here - developer.apple.com/documentation/swift/array/3017522-map
so can i remove any specific string from array using map? @PGDev
@Hamza For that you can use filter
@Hamza Here you can read about all higher-order functions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.