Search code examples
iosstringswift2xcode7

I have created one string extension to join string with space in swift 2.0 but its not working in swift 2.1 xcode 7


I am new in swift 2.1 , i have created one extension for string and function name is join, When i converted the code from swift 2.0 to swift 2.1 my code return me error please help me , i am not able to understand what to do

This is extension

extension String {
    func join<S : SequenceType where S.Generator.Element : Printable>(elements: S) -> String {
        return self.join(map(elements){ $0.description })
    }


}

Here is I am using like this

var channel_string:String!
        var languages = [String]()
        for var i = 0 ; i < ary_selected_channel.count ; i++
        {
           let getString_setvalue = ary_selected_channel.objectAtIndex(i) as! String
            languages.append(getString_setvalue)
        }
        channel_string = " ".join(languages)

The output is look like , assume In array i have Three name ["one","two","three"] then output is

channel_string = "one two three"

The error is 1) Printable has been renamed to customstringconv 2) Type of expression is ambiguous without more context


Solution

  • the error you receive is self-explanatory. by the way, there is easy to do the same without any complication ...

    let arr = ["one","two","three"]
    let str = arr.joined(separator: " ") //  "one two three"