In Swift, Array [String] slicing return type doesn't seem to be [String]

Liron Shapira picture Liron Shapira · Sep 7, 2014 · Viewed 34.5k times · Source

I'm slicing an array of strings and setting that to a [String] variable, but the type checker is complaining. Is it a possible compiler bug?

var tags = ["this", "is", "cool"]
tags[1..<3]
var someTags: [String] = tags[1..<3]

screenshot

Answer

Connor picture Connor · Sep 7, 2014

Subscripting an array with a range doesn't return an array, but a slice. You can create an array out of that slice though.

var tags = ["this", "is", "cool"]
tags[1..<3]
var someTags: Slice<String> = tags[1..<3]
var someTagsArray: [String] = Array(someTags)