91

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

0

6 Answers 6

174

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)
10
  • 3
    Where do you find Slice documented? Also the error message is incorrect and the Slice shows up as [String] in the playground. Thus it seems that there are two compiler errors and missing documentation.
    – zaph
    Sep 7, 2014 at 21:07
  • 1
    You can see the definition of the subscipt method here: developer.apple.com/library/prerelease/ios/documentation/… Sep 7, 2014 at 21:12
  • 2
    I agree that the error is incorrect and documentation could use some work. If you try it in two steps: var someTags = tags[1..<3] var someTagsArray: [String] = someTags it gives you the more helpful error: 'Slice<String>' is not convertible to '[String]' Sep 7, 2014 at 21:13
  • My fault, I looked at the Swift iBooks. I think Apple should remove them of keep them up-to-date.
    – zaph
    Sep 7, 2014 at 21:37
  • 1
    With Swift 3 I'm getting an error on the third line, "Type 'String' does not conform to protocol 'indexable'".
    – RenniePet
    Dec 29, 2016 at 7:07
14
var tags = ["this", "is", "cool"]
var someTags: [String] = Array(tags[1..<3])
println("someTags: \(someTags)") // "someTags: [is, cool]"
1

Another way to do that in one place is combine variable declaration let someTags: [String] and map(_:), that will transform ArraySlice<String> to [String]:

let tags = ["this", "is", "cool"]
let someTags: [String] = tags[1..<3].map { $0 } // ["is", "cool"]
0

You can also do this to get a new array of the slice:

var tags = ["this", "is", "cool"]
var someTags = [String]()
someTags += tags[1..<3]
println(someTags[0])  //prints ["is", "cool"]
2
  • It's OK, but it's confusing.
    – DawnSong
    Mar 1, 2017 at 3:45
  • I expected it'll print "is"
    – Yitzchak
    Sep 12, 2017 at 15:03
0

Another convenient way to convert an ArraySlice to Array is this:

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

It's not perfect because another developer (or yourself) who looks at it later may not understand its purpose. The good news is that if that developer (maybe you) removes the + [] they will immediately be met with a compiler error, which will hopefully clarify its purpose.

0

Just cast the slice as an Array when it's created. Keeping your Array as an array without having to use an intermediate variable. This works great when using Codable types.

var tags = ["this", "is", "cool"]
tags = Array(tags[1..<3])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.