实例方法
prefix(up
prefix(upTo:)
返回从集合开始到指定位置的子序列,但不包括指定位置。
声明
func prefix(upTo end: Self.Index) -> Self.SubSequence
返回值
直到但不包括 end
位置的子序列。
参数
end
结果子序列的“past the end” 索引。
end
必须是集合的有效索引。
详述
生成的子序列 does not include
位于位置 end
的元素。以下示例在整数数组中搜索数字 40
的索引,然后打印数组的前缀,直到但不包括该索引:
let numbers = [10, 20, 30, 40, 50, 60]
if let i = numbers.firstIndex(of: 40) {
print(numbers.prefix(upTo: i))
}
// Prints "[10, 20, 30]"
将集合的起始索引作为 end
参数传递会导致空子序列。
print(numbers.prefix(upTo: numbers.startIndex))
// Prints "[]"
使用prefix(upTo:)
方法相当于使用部分半开范围作为集合的下标。下标符号优于 prefix(upTo:)
。
if let i = numbers.firstIndex(of: 40) {
print(numbers[..<i])
}
// Prints "[10, 20, 30]"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift Dictionary.Keys prefix(_:)用法及代码示例
- Swift Dictionary.Keys prefix(through:)用法及代码示例
- Swift Dictionary.Keys shuffled()用法及代码示例
- Swift Dictionary.Keys dropFirst(_:)用法及代码示例
- Swift Dictionary.Keys firstIndex(where:)用法及代码示例
- Swift Dictionary.Keys description用法及代码示例
- Swift Dictionary.Keys contains(_:)用法及代码示例
- Swift Dictionary.Keys reduce(_:_:)用法及代码示例
- Swift Dictionary.Keys first用法及代码示例
- Swift Dictionary.Keys suffix(_:)用法及代码示例
- Swift Dictionary.Keys randomElement()用法及代码示例
- Swift Dictionary.Keys dropLast(_:)用法及代码示例
- Swift Dictionary.Keys flatMap(_:)用法及代码示例
- Swift Dictionary.Keys joined()用法及代码示例
- Swift Dictionary.Keys enumerated()用法及代码示例
- Swift Dictionary.Keys starts(with:)用法及代码示例
- Swift Dictionary.Keys indices用法及代码示例
- Swift Dictionary.Keys shuffled(using:)用法及代码示例
- Swift Dictionary.Keys max()用法及代码示例
- Swift Dictionary.Keys split(separator:maxSplits:omittingEmptySubsequences:)用法及代码示例
- Swift Dictionary.Keys firstIndex(of:)用法及代码示例
- Swift Dictionary.Keys max(by:)用法及代码示例
- Swift Dictionary.Keys allSatisfy(_:)用法及代码示例
- Swift Dictionary.Keys map(_:)用法及代码示例
- Swift Dictionary.Keys reduce(into:_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Dictionary.Keys prefix(upTo:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。