|
| 1 | +--- |
| 2 | +Title: 'dropFirst()' |
| 3 | +Description: 'Returns a subsequence by skipping the first element or first n elements from a Swift collection.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Mobile Development' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Collections' |
| 10 | + - 'Swift' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-swift' |
| 13 | + - 'paths/build-ios-apps-with-swiftui' |
| 14 | +--- |
| 15 | + |
| 16 | +In Swift, the **`dropFirst()`** method is used on collections like [arrays](https://www.codecademy.com/resources/docs/swift/arrays), strings, and other sequences to return a subsequence with the first element (or first _n_ elements) removed. It doesn’t mutate the original collection, but returns a new subsequence starting from the dropped position. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +collection.dropFirst(n) |
| 22 | +``` |
| 23 | + |
| 24 | +**Parameters:** |
| 25 | + |
| 26 | +- `n` (Optional): An `Int` representing the number of elements to skip. If omitted, it defaults to 1. |
| 27 | + |
| 28 | +**Return value:** |
| 29 | + |
| 30 | +Returns a `SubSequence` meaning a view into the original collection with the specified number of leading elements removed. |
| 31 | + |
| 32 | +## Example 1: Removing the First Element from an Array |
| 33 | + |
| 34 | +This example drops the first item in an array of numbers: |
| 35 | + |
| 36 | +```swift |
| 37 | +let scores = [90, 85, 80, 75] |
| 38 | +let updatedScores = scores.dropFirst() |
| 39 | + |
| 40 | +print(updatedScores) |
| 41 | +``` |
| 42 | + |
| 43 | +The output of this code is: |
| 44 | + |
| 45 | +```shell |
| 46 | +[85, 80, 75] |
| 47 | +``` |
| 48 | + |
| 49 | +## Example 2: Dropping the First 3 Names from a List |
| 50 | + |
| 51 | +This example skips the first three values in an array of strings: |
| 52 | + |
| 53 | +```swift |
| 54 | +let names = ["Alice", "Bob", "Charlie", "Dana", "Eve"] |
| 55 | +let remaining = names.dropFirst(3) |
| 56 | + |
| 57 | +print(remaining) |
| 58 | +``` |
| 59 | + |
| 60 | +The output of this code is: |
| 61 | + |
| 62 | +```shell |
| 63 | +["Dana", "Eve"] |
| 64 | +``` |
| 65 | + |
| 66 | +## Example 3: Converting Result Back to an Array |
| 67 | + |
| 68 | +Since `dropFirst()` returns a `SubSequence`, use `Array()` to convert the result when you need an actual array: |
| 69 | + |
| 70 | +```swift |
| 71 | +let data = [1, 2, 3, 4, 5] |
| 72 | +let trimmed = Array(data.dropFirst(2)) |
| 73 | + |
| 74 | +print(trimmed) |
| 75 | +``` |
| 76 | + |
| 77 | +The output of this code is: |
| 78 | + |
| 79 | +```shell |
| 80 | +[3, 4, 5] |
| 81 | +``` |
| 82 | + |
| 83 | +## Frequently asked questions |
| 84 | + |
| 85 | +### 1. What is the type of an array in Swift? |
| 86 | + |
| 87 | +In Swift, arrays are generic collections. This means the type of array depends on the type of elements it holds. For example: |
| 88 | + |
| 89 | +- `let numbers: [Int] = [1, 2, 3]` is an array of integers (`[Int]`) |
| 90 | +- `let names: [String] = ["Alice", "Bob"]` is an array of strings (`[String]`) |
| 91 | + |
| 92 | +### 2. Are Swift arrays dynamic? |
| 93 | + |
| 94 | +Yes, Swift arrays are dynamic. This means they can grow or shrink in size: |
| 95 | + |
| 96 | +- You can append new elements using `.append()` or `+=` |
| 97 | +- You can remove elements using `.remove(at:)`, `.removeFirst()`, etc. |
| 98 | + |
| 99 | +### 3. What is the difference between `dropFirst()` and `removeFirst()` in Swift? |
| 100 | + |
| 101 | +- `dropFirst()` returns a new array after skipping the first element (or a specified number of elements) without modifying the original array. |
| 102 | +- `removeFirst()` removes and returns the first element (or first `n` elements) from the original array, modifying it in place. |
0 commit comments