Swift: how can String.join() work custom types?

后端 未结 6 553
感动是毒
感动是毒 2020-12-30 02:43

for example:

var a = [1, 2, 3]    // Ints
var s = \",\".join(a)  // EXC_BAD_ACCESS

Is it possible to make the join function return \"1,2,3\

6条回答
  •  盖世英雄少女心
    2020-12-30 03:22

    A Swift 3 solution

    public extension Sequence where Iterator.Element: CustomStringConvertible {
        func joined(seperator: String) -> String {
            return self.map({ (val) -> String in
                "\(val)"
            }).joined(separator: seperator)
        }
    }
    

提交回复
热议问题