0

I'd like to create an extension function on 'Array' that inserts an array at the index provided.

mutating func insert<T>( array: [T], atIndex index: Int ) {
    var floatingIndex = index
    for (index, value) in enumerate(array) {
        self.insert(value, atIndex: floatingIndex)
        floatingIndex++
    }
}

Something like this, but I get an error saying I can't invoke 'insert(...)' with an argument of type (T, atIndex: Int).

Is there a way I can insert a generic into an array?

SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
  • 1
    `extension Array { mutating func insert( array: Array, atIndex index: Int ) { var floatingIndex = index for value in array { self.insert(value, atIndex: floatingIndex) floatingIndex++ } } }` – chuthan20 Jun 10 '15 at 14:41
  • 1
    As explained in the answer to the duplicate, you just have to remove the `` from the method signature, then it compiles and works as expected. – Note that you can achieve the same with `array.replaceRange(insertIndex ..< insertIndex, with: otherArray)`. – Martin R Jun 10 '15 at 14:41

0 Answers0