<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Sequence/prefix(while:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:STsE6prefix5whileSay7ElementQzGSbADKXE_tKF"
  },
  "title" : "prefix(while:)"
}
-->

# prefix(while:)

Returns a sequence containing the initial, consecutive elements that
satisfy the given predicate.

```
func prefix(while predicate: (Self.Element) throws -> Bool) rethrows -> [Self.Element]
```

## Parameters

`predicate`

A closure that takes an element of the sequence as
its argument and returns a Boolean value indicating whether the
element should be included in the result.

## Return Value

A sequence of the initial, consecutive elements that
satisfy `predicate`.

## Discussion

The following example uses the `prefix(while:)` method to find the
positive numbers at the beginning of the `numbers` array. Every element
of `numbers` up to, but not including, the first negative value is
included in the result.

```
let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
let positivePrefix = numbers.prefix(while: { $0 > 0 })
// positivePrefix == [3, 7, 4]
```

If `predicate` matches every element in the sequence, the resulting
sequence contains every element of the sequence.

> Complexity: O(*k*), where *k* is the length of the result.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)