Swift: Convert a sequence to an array

From the department of overthinking department, you can just use Array(1…5) and Array(“Hello”). Oh well, move on move on. 🙂 Thanks Davide De F. & Ray Fix

Trick du jour, e.g. ToArray(1…5)


  • but you already can do Array(1 … 5)

    • Can you do Array(“Hello”)? Oh yes, you can! Never mind, move along, move along. 🙂

      • In the Swift 2.1 playground and REPL, Array(“Hello”) results in “error: missing argument label ‘arrayLiteral:’ in call” but Array(“Hello”.characters) works.

  • I was playing with a minimal way to generate an array of ascending objects:

    given a class decl such as:
    class mc {
    let k : Int
    init(inK:Int) { k = inK }

    let objArray = (1…3).map{ x in mc(inK:x) }

  • Updated a bit for Swift 3 and as an extension:

    extension Sequence {

    func toArray() -> [Iterator.Element] {

    return Array(self)