The problem is that mentions[0...3] returns an ArraySlice<String>, not an Array<String>. Therefore you could first use the Array(_:) initialiser in order to convert the slice into an array:

let first3Elements : [String] // An Array of up to the first 3 elements.
if mentions.count >= 3 {
    first3Elements = Array(mentions[0 ..< 3])
} else {
    first3Elements = mentions
}

Or if you want to use an ArraySlice (they are useful for intermediate computations, as they present a 'view' onto the original array, but are not designed for long term storage), you could subscript mentions with the full range of indices in your else:

let slice : ArraySlice<String> // An ArraySlice of up to the first 3 elements
if mentions.count >= 3 {
    slice = mentions[0 ..< 3]
} else {
    slice = mentions[mentions.indices] // in Swift 4: slice = mentions[...]
}

Although the simplest solution by far would be just to use the prefix(_:) method, which will return an ArraySlice of the first n elements, or a slice of the entire array if n exceeds the array count:

let slice = mentions.prefix(3) // ArraySlice of up to the first 3 elements
Answer from Hamish on Stack Overflow
🌐
Swift by Sundell
swiftbysundell.com › articles › slicing-swift-collections
Slicing Swift collections | Swift by Sundell
In Swift, a slice is a special kind of collection that doesn’t actually store any elements of its own, but rather acts as a proxy (or view) to enable us access and work with a subset of another collection as a separate instance.
🌐
Swift Forums
forums.swift.org › using swift
Functions that accept arrays or array slices - Using Swift - Swift Forums
April 29, 2022 - I am working on a Big Integer package with emphasis on performance. My current design follows what most others have done, i.e. the Big Integer is a UInt64 array with functions contained in a Uint64 array extension. All…
🌐
Hacking with Swift
hackingwithswift.com › example-code › language › how-to-split-an-array-into-chunks
How to split an array into chunks - free Swift example code and tips
May 28, 2019 - extension Array { func chunked(into size: Int) -> [[Element]] { return stride(from: 0, to: count, by: size).map { Array(self[$0 ..< Swift.min($0 + size, count)]) } } }
🌐
Medium
medium.com › appcoda-tutorials › understanding-the-arrayslice-3b4957b9d965
Understanding The ArraySlice in Swift | by Jimmy M Andersson | AppCoda Tutorials | Medium
June 9, 2019 - Now we’ve got an ArraySlice instance. Remember that it doesn’t create its own copy of the 2, 3, and 4 that it claims to hold, it merely borrows that portion of the array variable’s memory. Let’s see if we can use our new slice variable for something useful, shall we?
🌐
Swdevnotes
swdevnotes.com › swift › 2023 › arrayslice-with-range-operator-and-prefix-in-swift
ArraySlice with range operator and Prefix in Swift | Software Development Notes
ArraySlice is used in Swift to return a view on a subset of a collection without the overhead of making a copy of the collection. The half-open range (..<) and closed range (...) operators are used to specify the index range to retrieve.
🌐
DZone
dzone.com › data engineering › data › arrayslice in swift
ArraySlice in Swift
October 11, 2017 - Fortunately, Swift allows us to cast the slice variable using the syntax Array(<slice_variable>).
Find elsewhere
🌐
Sarunw
sarunw.com › posts › how-to-get-first-n-elements-of-swift-array
How to get the first N elements of array in Swift | Sarunw
December 10, 2020 - Unlike an array, ArraySlice may have a nonzero startIndex and an endIndex that is not equal to count since it uses the same indices of the original array. The following example gets a slice of elements index 1 and 2 from the names array.
🌐
Swift Forums
forums.swift.org › using swift
How to get the base array from an `ArraySlice`? - Using Swift - Swift Forums
September 1, 2021 - Slice and Substring have a convenient base property, which is useful for getting access to the whole collection, but ArraySlice seems to have this functionality omitted. Is this intentional? Is it possible to get the who…
🌐
Swift Forums
forums.swift.org › evolution › discussion
Slicing should return Array by default rather than the confusing and error-prone ArraySlice - Discussion - Swift Forums
September 12, 2018 - ArraySlice is confusing and error-prone since indexing doesn't necessarily start at zero and many functions require arrays rather than slices as parameters. ArraySlice is also a form of premature optimization. IMO, it would be better for slicing to return an Array.
🌐
Swift Forums
forums.swift.org › using swift
ArraySlice creates undesired copies - Using Swift - Swift Forums
August 26, 2022 - ArraySlice breaks the promise of being a view into a larger array. Instead, creates copies. Consider the following example: func test(_ slice: inout ArraySlice ) { var payload = slice.dropFirst(1) payload.withUnsafeMutableBytes { bufferPointer in bufferPointer.storeBytes(of: 8, as: UInt8.self) } print("payload: \(payload)") } var buffer = [UInt8](repeating: 0, count: 10); test(&buffer[5...]); print("buffer = \(buffer)") slice.dropFirst(1) returns ArraySlice
🌐
W3Schools
w3schools.com › swift › swift_arrays_slices.asp
Swift Arrays - Slices
Strings Concatenation Numbers and Strings Special Characters Unicode & Scalars Swift Arrays · Arrays Loop Through an Array Array Slices Indices & Bounds Multidimensional Arrays Real-Life Examples Swift Ranges Swift If...Else
🌐
Swift Forums
forums.swift.org › using swift
Recursively slice an array - Using Swift - Swift Forums
June 8, 2021 - I noticed strange behavior in type System. Look at next functions func drop() { let a = [1, 2, 3] let b = a.dropLast() // Array .SubSequence handle(a: b) } func handle(a: [Int]) { } It does not compile, because b is type of ArraySlice while the function handle(a: [Int]) wants the type [Int]. BUT If we are slicing an array within handle(a: [Int]) the code compiles and works func run() { handle(a: [1, 2, 3]) } func handl...
🌐
Medium
medium.com › @mimicatcodes › array-and-arrayslice-in-swift-3-aaa6841d3119
Array and ArraySlice in Swift 3. In this post, we will learn how to grab… | by Luna An | Medium
June 15, 2017 - In this post, we will learn how to grab sub arrays out of arrays in Swift — and they are considered type ArraySlice. ... A slice of an Array, ContiguousArray, or ArraySlice instance.
🌐
Medium
medium.com › @marcosantadev › arrayslice-in-swift-4e18522ab4e4
ArraySlice In Swift. Original link… | by Marco Santarossa | Medium
October 9, 2017 - Fortunately, Swift allows us to cast the slice variable using the syntax Array(<slice_variable>).
🌐
TutorialsPoint
tutorialspoint.com › new-array-from-index-range-swift
New Array from Index Range Swift
In the end, we will convert the ArraySlice to an array using the Array initializer and print the result. import Foundation let numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] let startIndex = 3 // inclusive let endIndex = 8 // exclusive let slice = numbers[startIndex..<endIndex] // return an ArraySlice<Int> let newArray = Array(slice) // converts ArraySlice<Int> to [Int] print("Original array: \(numbers)") print("New array: \(newArray)")
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › slice
Array.prototype.slice() - JavaScript | MDN
The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
🌐
GitHub
github.com › apple › swift › blob › main › stdlib › public › core › ArraySlice.swift
swift/stdlib/public/core/ArraySlice.swift at main · swiftlang/swift
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors · // //===----------------------------------------------------------------------===// // // - `ArraySlice<Element>` presents an arbitrary subsequence of some · // contiguous sequence of `Element`s. // //===----------------------------------------------------------------------===// · /// A slice of an `Array`, `ContiguousArray`, or `ArraySlice` instance.
Author   swiftlang