Returns a sequence containing the initial, consecutive elements that satisfy the given predicate.
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
- Xcode 11.0+
- Apple Crypto
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.
A sequence of the initial, consecutive elements that satisfy
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.
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.