Array.prefix() / suffix() / dropFirst() / dropLast()
In Swift arrays, prefix() and suffix() retrieve elements from the beginning or end, while dropFirst() and dropLast() return the remaining elements after dropping from either end. You can also concatenate arrays using the + operator or joined().
Syntax
// Get the first n elements
array.prefix(n)
array.prefix(while: { condition })
// Get the last n elements
array.suffix(n)
// Get all elements except the first n
array.dropFirst(n)
array.dropFirst(while: { condition })
// Get all elements except the last n
array.dropLast(n)
// Concatenate arrays
array1 + array2
array.joined() // Flattens [[Int]] into [Int]
array.joined(separator: separator)
Method List
| Method | Description |
|---|---|
| 配列.prefix(n) | Returns an ArraySlice of the first n elements. |
| 配列.prefix(while:) | Returns leading elements that satisfy the condition (stops at the first false result). |
| 配列.suffix(n) | Returns an ArraySlice of the last n elements. |
| 配列.dropFirst() | Returns an ArraySlice of all elements except the first. |
| 配列.dropFirst(n) | Returns an ArraySlice of all elements except the first n. |
| 配列.dropLast(n) | Returns an ArraySlice of all elements except the last n. |
| 配列.dropLast(while:) | Returns the elements after dropping trailing elements that satisfy the condition. |
| 配列1 + 配列2 | Returns a new array by concatenating two arrays. |
| 配列.joined() | Flattens a nested array ([[T]]) into a flat array ([T]). |
Sample Code
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// prefix: first n elements
let first3 = Array(numbers.prefix(3))
print(first3) // [1, 2, 3]
// prefix(while:): leading elements matching the condition
let lessThan5 = Array(numbers.prefix(while: { $0 < 5 }))
print(lessThan5) // [1, 2, 3, 4]
// suffix: last n elements
let last3 = Array(numbers.suffix(3))
print(last3) // [8, 9, 10]
// dropFirst: skip the first n elements
let withoutFirst = Array(numbers.dropFirst(3))
print(withoutFirst) // [4, 5, 6, 7, 8, 9, 10]
// dropLast: skip the last n elements
let withoutLast = Array(numbers.dropLast(3))
print(withoutLast) // [1, 2, 3, 4, 5, 6, 7]
// Concatenate arrays
let a = [1, 2, 3]
let b = [4, 5, 6]
let combined = a + b
print(combined) // [1, 2, 3, 4, 5, 6]
var c = [7, 8]
c += [9, 10]
print(c) // [7, 8, 9, 10]
// joined: flatten a nested array
let nested = [[1, 2], [3, 4], [5, 6]]
let flat = Array(nested.joined())
print(flat) // [1, 2, 3, 4, 5, 6]
// joined with a string array
let words = [["Hello", "World"], ["Swift", "is", "great"]]
let sentence = words.joined(separator: ["|"]).joined(separator: " ")
print(sentence) // Hello World | Swift is great
Notes
prefix(), suffix(), dropFirst(), and dropLast() return an ArraySlice type, which is a view into the original array. If you need an independent array, copy it with Array(...).
The combination of prefix and dropFirst is useful for implementing pagination. If the requested count exceeds the array bounds (e.g., calling prefix(20) on a 10-element array), only the available elements are returned — no crash occurs.
For adding and removing elements, see 配列.append() / insert() / remove().
If you find any errors or copyright issues, please contact us.