實例方法
advanced(by:)
返回一個偏離該值指定距離的值。
聲明
func advanced(by n: Int) -> UnsafeRawPointer
返回值
從該值偏移 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 UnsafeRawPointer ...(_:_:)用法及代碼示例
- Swift UnsafeRawPointer ..<(_:)用法及代碼示例
- Swift UnsafeRawPointer bindMemory(to:capacity:)用法及代碼示例
- Swift UnsafeRawPointer ...(_:)用法及代碼示例
- Swift UnsafeRawPointer withMemoryRebound(to:capacity:_:)用法及代碼示例
- Swift UnsafeRawPointer ..<(_:_:)用法及代碼示例
- Swift UnsafeRawPointer用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator dropFirst(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator max()用法及代碼示例
- Swift UnsafeRawBufferPointer shuffled(using:)用法及代碼示例
- Swift UnsafeRawBufferPointer isEmpty用法及代碼示例
- Swift UnsafeRawBufferPointer enumerated()用法及代碼示例
- Swift UnsafeRawBufferPointer subscript(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer starts(with:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator filter(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer firstIndex(where:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator drop(while:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator min()用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator contains(where:)用法及代碼示例
- Swift UnsafeRawBufferPointer contains(where:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator contains(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator reduce(into:_:)用法及代碼示例
- Swift UnsafeRawBufferPointer first(where:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator map(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer allSatisfy(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 UnsafeRawPointer advanced(by:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。