Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join an array of strings into a single string Swift 4

Prior to Swift 4 I used the code below to join an array of strings into a single string:

let array = ["Andrew", "Ben", "John", "Paul", "Peter", "Laura"]
let joined = array.joined(separator: ", ")

 //output:- "Andrew, Ben, John, Paul, Peter, Laura"

But now in Swift 4 and Xcode 9 is showing one of the following errors:

Ambiguous reference to member 'joined()'

Value of type 'TYPE' has no member 'join'

How can I join all elements of an array? Was the syntax changed in Swift 4?

enter image description here

like image 248
Arjun Yadav Avatar asked Sep 23 '17 10:09

Arjun Yadav


People also ask

How do you join strings together in Swift?

If you have an array of strings and want to merge all the elements together into a single string, it's just one line of code in Swift thanks to the joined () method. let array = ["Andrew", "Ben", "John", "Paul", "Peter", "Laura"] let joined = array.joined(separator: ", ") The result is that joined is set to "Andrew, Ben, John, Paul, Peter, Laura".

How to join an array of strings into a string in Java?

How to join an array of strings into a string in Java. To join an array of strings into a single string we can use the join () method in Java. In this below example, we are joining each string in an array without any delimiter. If you want a string which is separated by the delimiter like comma , you need to specify in the join () method.

How do you join two strings together in Python?

The join operator concatenates a set of strings into a single string. The strings are separated with a delimiter and appended into the result string. The following command joins the array of strings into a single string as comma (,) separated values. Likewise, you can use any delimiter character and combine the strings.

How to convert an array to singe delimited string?

If you need to convert an array to singe delimited string it is easy to do. Below is an example C# code to demonstrate how to join an array of integer values into a single delimited string: I am using the string.Join () to join my array, the first parameter would be the delimiter character for this example I am using the comma “,”.


1 Answers

That's a typical example where pseudo-code in the question is misleading and doesn't reproduce the error.

The error occurs because you are using flatMap. Since the array is a non-optional single-level array of Int just use map and don't use the describing initializer:

func getNumbers(array : [Int]) -> String {
    let stringArray = array.map{ String($0) }
    return stringArray.joined(separator: ",")
}

The ambiguity is that flatMap applied to an non-optional sequence has a different meaning:

From the documentation

let numbers = [1, 2, 3, 4]

let mapped = numbers.map { Array(count: $0, repeatedValue: $0) } 
// [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]]

let flatMapped = numbers.flatMap { Array(count: $0, repeatedValue: $0) }
 // [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]

In fact, s.flatMap(transform) is equivalent to Array(s.map(transform).joined()).

like image 118
vadian Avatar answered Sep 27 '22 19:09

vadian