枚举
Unbounded
UnboundedRange_
表示集合的整个范围的范围表达式。
声明
@frozen enum UnboundedRange_
概述
您可以使用无界范围运算符 (...
) 创建包含所有集合元素的集合切片。无限范围切片本质上是将集合实例转换为其切片类型。
例如,以下代码声明了 countLetterChanges(_:_:)
,该函数用于查找将一个单词或短语更改为另一个所需的更改次数。该函数使用递归方法对越来越小的原始字符串执行相同的比较。为了使用递归而不在每一步复制字符串,countLetterChanges(_:_:)
使用字符串的切片类型 Substring
作为其参数。
func countLetterChanges(_ s1: Substring, _ s2: Substring) -> Int {
if s1.isEmpty { return s2.count }
if s2.isEmpty { return s1.count }
let cost = s1.first == s2.first ? 0 : 1
return min(
countLetterChanges(s1.dropFirst(), s2) + 1,
countLetterChanges(s1, s2.dropFirst()) + 1,
countLetterChanges(s1.dropFirst(), s2.dropFirst()) + cost)
}
要使用两个字符串调用 countLetterChanges(_:_:)
,请在每个字符串的下标中使用无界范围。
let word1 = "grizzly"
let word2 = "grisly"
let changes = countLetterChanges(word1[...], word2[...])
// changes == 2
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift Unicode.CanonicalCombiningClass ...(_:_:)用法及代码示例
- Swift UnsafeMutableRawPointer ..<(_:_:)用法及代码示例
- Swift UnfoldSequence shuffled()用法及代码示例
- Swift UnsafeBufferPointer randomElement()用法及代码示例
- Swift UnsafeRawBufferPointer.Iterator dropFirst(_:)用法及代码示例
- Swift UnsafeMutablePointer allocate(capacity:)用法及代码示例
- Swift UnsafeRawBufferPointer.Iterator max()用法及代码示例
- Swift UnsafeBufferPointer min(by:)用法及代码示例
- Swift Unicode.Scalar.UTF16View contains(where:)用法及代码示例
- Swift UnsafeMutableBufferPointer allocate(capacity:)用法及代码示例
- Swift UnsafeRawBufferPointer shuffled(using:)用法及代码示例
- Swift UnsafeBufferPointer prefix(upTo:)用法及代码示例
- Swift Unicode.Scalar.UTF8View dropFirst(_:)用法及代码示例
- Swift Unicode.Scalar.UTF16View firstIndex(of:)用法及代码示例
- Swift UnsafeMutableRawPointer ...(_:_:)用法及代码示例
- Swift UnsafeMutableBufferPointer prefix(through:)用法及代码示例
- Swift Unicode.Scalar.UTF16View randomElement()用法及代码示例
- Swift UnsafeRawBufferPointer isEmpty用法及代码示例
- Swift Unicode.Scalar.UTF8View contains(where:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer shuffle()用法及代码示例
- Swift UnsafeRawPointer advanced(by:)用法及代码示例
- Swift Unicode.UTF32 encode(_:into:)用法及代码示例
- Swift Unicode.Scalar.UTF16View forEach(_:)用法及代码示例
- Swift UnsafeRawBufferPointer enumerated()用法及代码示例
- Swift UnsafeBufferPointer firstIndex(of:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 UnboundedRange_。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。