As per Doc:
extension Array where Element == String {
/// Returns a new string by concatenating the elements of the sequence,
/// adding the given separator between each element.
///
/// The following example shows how an array of strings can be joined to a
/// single, comma-separated string:
///
/// let cast = ["Vivien", "Marlon", "Kim", "Karl"]
/// let list = cast.joined(separator: ", ")
/// print(list)
/// // Prints "Vivien, Marlon, Kim, Karl"
///
/// - Parameter separator: A string to insert between each of the elements
/// in this sequence. The default separator is an empty string.
/// - Returns: A single, concatenated string.
public func joined(separator: String = default) -> String
}
It will only work with Array<String>
And your code should be:
func arrayToString (_ x:Array<String>) -> String {
let y = x.joined(separator: "")
return y
}
arrayToString(["abc", "123"]) //"abc123"
xare of typeAny, a type you'd generally want to avoid (unless forced to use due to being suppliedAnyobjects from some API at runtime). Is it an option to letxbe an array ofStringinstances instead? ([String]). Also, you haven't supplied a return type to your function, so it is inferred to beVoid(you want this to be-> String). If you stick withxbeing[Any], you need to perform an attempted type conversion of each element inxtoStringprior to callingjoined(separator:), e.g.return x.flatMap{ $0 as? String }.joined(separator: "").