Instance Methodswift 6.0.1Swift
prefix(upTo:)
Returns a subsequence from the start of the collection up to, but not including, the specified position.
func prefix(upTo end: Self.Index) -> Self.SubSequence
Parameters
- end
The “past the end” index of the resulting subsequence.
end
must be a valid index of the collection.
Returns
A subsequence up to, but not including, the end
position.
The resulting subsequence does not include the element at the position end
. The following example searches for the index of the number 40
in an array of integers, and then prints the prefix of the array up to, but not including, that index:
let numbers = [10, 20, 30, 40, 50, 60]
if let i = numbers.firstIndex(of: 40) {
print(numbers.prefix(upTo: i))
}
// Prints "[10, 20, 30]"
Passing the collection’s starting index as the end
parameter results in an empty subsequence.
print(numbers.prefix(upTo: numbers.startIndex))
// Prints "[]"
Using the prefix(upTo:)
method is equivalent to using a partial half-open range as the collection’s subscript. The subscript notation is preferred over prefix(upTo:)
.
if let i = numbers.firstIndex(of: 40) {
print(numbers[..<i])
}
// Prints "[10, 20, 30]"
Other members in extension
Typealiases
Show obsolete interfaces (1)
Hide obsolete interfaces
Instance members
var count: Int
The number of elements in the collection.
var first: Self.Element?
The first element of the collection.
var isEmpty: Bool
A Boolean value indicating whether the collection is empty.
var underestimatedCount: Int
A value less than or equal to the number of elements in the collection.
subscript<R>(R
) -> Self.SubSequence Accesses the contiguous subrange of the collection’s elements specified by a range expression.
subscript((UnboundedRange_) -> ()
) -> Self.SubSequence subscript(RangeSet<Self.Index>
) -> DiscontiguousSlice<Self> Accesses a view of this collection with the elements at the given indices.
func distance(from: Self.Index, to: Self.Index
) -> Int Returns the distance between two indices.
func drop(while: (Self.Element) throws -> Bool
) rethrows -> Self.SubSequence Returns a subsequence by skipping elements while
predicate
returnstrue
and returning the remaining elements.func dropFirst(Int
) -> Self.SubSequence Returns a subsequence containing all but the given number of initial elements.
func dropLast(Int
) -> Self.SubSequence Returns a subsequence containing all but the specified number of final elements.
func firstIndex(where: (Self.Element) throws -> Bool
) rethrows -> Self.Index? Returns the first index in which an element of the collection satisfies the given predicate.
func formIndex(inout Self.Index, offsetBy: Int
) Offsets the given index by the specified distance.
func formIndex(inout Self.Index, offsetBy: Int, limitedBy: Self.Index
) -> Bool Offsets the given index by the specified distance, or so that it equals the given limiting index.
func formIndex(after: inout Self.Index
) Replaces the given index with its successor.
func index(Self.Index, offsetBy: Int
) -> Self.Index Returns an index that is the specified distance from the given index.
func index(Self.Index, offsetBy: Int, limitedBy: Self.Index
) -> Self.Index? Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.
func indices(where: (Self.Element) throws -> Bool
) rethrows -> RangeSet<Self.Index> Returns the indices of all the elements that match the given predicate.
func map<T, E>((Self.Element)
throws Returns an array containing the results of mapping the given closure over the sequence’s elements.
func prefix(Int
) -> Self.SubSequence Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.
func prefix(through: Self.Index
) -> Self.SubSequence Returns a subsequence from the start of the collection through the specified position.
func prefix(while: (Self.Element) throws -> Bool
) rethrows -> Self.SubSequence Returns a subsequence containing the initial elements until
predicate
returnsfalse
and skipping the remaining elements.func randomElement(
) -> Self.Element? Returns a random element of the collection.
func randomElement<T>(using: inout T
) -> Self.Element? Returns a random element of the collection, using the given generator as a source for randomness.
func removingSubranges(RangeSet<Self.Index>
) -> DiscontiguousSlice<Self> Returns a collection of the elements in this collection that are not represented by the given range set.
func split(maxSplits: Int, omittingEmptySubsequences: Bool, whereSeparator: (Self.Element) throws -> Bool
) rethrows -> [Self.SubSequence] Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate.
func suffix(Int
) -> Self.SubSequence Returns a subsequence, up to the given maximum length, containing the final elements of the collection.
func suffix(from: Self.Index
) -> Self.SubSequence Returns a subsequence from the specified position to the end of the collection.
Show obsolete interfaces (6)
Hide obsolete interfaces
func distance<T>(from: Self.Index, to: Self.Index
) -> T func flatMap((Self.Element) throws -> String?
) rethrows -> [String] func formIndex<T>(inout Self.Index, offsetBy: T
) func formIndex<T>(inout Self.Index, offsetBy: T, limitedBy: Self.Index
) -> Bool func index<T>(Self.Index, offsetBy: T
) -> Self.Index func index<T>(Self.Index, offsetBy: T, limitedBy: Self.Index
) -> Self.Index?