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

后端 未结 6 512
感动是毒
感动是毒 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:23

    And just to make your life more complete, starting from Xcode 8.0 beta 1 in Swift 3 you should NOW use [String].joined(separator: ",").

    This is the new "ed/ing" naming rule for Swift APIs:

    Name functions and methods according to their side-effects

    • Those without side-effects should read as noun phrases, e.g. x.distance(to: y), i.successor().
    • Those with side-effects should read as imperative verb phrases, e.g., print(x), x.sort(), x.append(y).
    • Name Mutating/nonmutating method pairs consistently. A mutating method will often have a nonmutating variant with similar semantics, but that returns a new value rather than updating an instance in-place. Swift: API Design Guidelines

提交回复
热议问题