實例方法
advanced(by:)
返回一個偏離該值指定距離的值。
必需的。提供默認實現。
聲明
func advanced(by n: Self.Stride) -> Self
返回值
從該值偏移 n
的值。
參數
n
推進此值的距離。
詳述
使用通用代碼中的 advanced(by:)
方法將值偏移指定距離。如果您直接使用數值,請使用加法運算符 (+
) 而不是此方法。
func addOne<T: Strideable>(to x: T) -> T
where T.Stride: ExpressibleByIntegerLiteral
{
return x.advanced(by: 1)
}
let x = addOne(to: 5)
// x == 6
let y = addOne(to: 3.5)
// y = 4.5
如果此類型的 Stride
類型符合 BinaryInteger
,則對於值 x
、距離 n
和值 y = x.advanced(by: n)
、x.distance(to: y) == n
。將此方法用於具有非整數 Stride
的類型可能會導致近似值。如果前進n
的結果不能表示為該類型的值,則可能會發生運行時錯誤。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift Strideable用法及代碼示例
- Swift StrideThrough map(_:)用法及代碼示例
- Swift StrideTo allSatisfy(_:)用法及代碼示例
- Swift StrideTo joined(separator:)用法及代碼示例
- Swift StrideTo drop(while:)用法及代碼示例
- Swift StrideTo filter(_:)用法及代碼示例
- Swift StrideThrough reduce(_:_:)用法及代碼示例
- Swift StrideTo sorted()用法及代碼示例
- Swift StrideTo split(separator:maxSplits:omittingEmptySubsequences:)用法及代碼示例
- Swift StrideTo min()用法及代碼示例
- Swift StrideTo dropLast(_:)用法及代碼示例
- Swift StrideTo forEach(_:)用法及代碼示例
- Swift StrideThrough shuffled()用法及代碼示例
- Swift StrideTo enumerated()用法及代碼示例
- Swift StrideThrough suffix(_:)用法及代碼示例
- Swift StrideThrough prefix(_:)用法及代碼示例
- Swift StrideTo suffix(_:)用法及代碼示例
- Swift StrideThrough contains(_:)用法及代碼示例
- Swift StrideThrough split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift StrideThrough prefix(while:)用法及代碼示例
- Swift StrideThrough sorted()用法及代碼示例
- Swift StrideTo reduce(_:_:)用法及代碼示例
- Swift StrideThrough max()用法及代碼示例
- Swift StrideTo first(where:)用法及代碼示例
- Swift StrideThrough max(by:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Strideable advanced(by:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。