实例方法
load(from
load(fromByteOffset:as:)
返回给定类型的新实例,从缓冲区指针的原始内存中以指定的字节偏移量读取。
声明
func load<T>(
fromByteOffset offset: Int = 0,
as type: T.Type
) -> T
返回值
T
类型的新实例,从缓冲区指针的内存中复制。
参数
offset
开始读取新实例数据的缓冲区指针内存的偏移量(以字节为单位)。缓冲区指针加上
offset
必须正确对齐才能访问类型为T
的实例。默认为零。type
用于新构造实例的类型。内存必须初始化为与
type
布局兼容的类型的值。
详述
您可以使用此方法从缓冲区指针的底层字节创建新值。以下示例从缓冲区指针 someBytes
引用的内存中创建两个新的 Int32
实例。 a
的字节是从 someBytes
的前四个字节复制的,而 b
的字节是从接下来的四个字节复制的。
let a = someBytes.load(as: Int32.self)
let b = someBytes.load(fromByteOffset: 4, as: Int32.self)
为新实例读取的内存不得超出缓冲区指针的内存区域,即 offset + MemoryLayout<T>.size
必须小于或等于缓冲区指针的 count
。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift UnsafeRawBufferPointer last用法及代码示例
- Swift UnsafeRawBufferPointer lastIndex(of:)用法及代码示例
- Swift UnsafeRawBufferPointer lexicographicallyPrecedes(_:)用法及代码示例
- Swift UnsafeRawBufferPointer lastIndex(where:)用法及代码示例
- Swift UnsafeRawBufferPointer last(where:)用法及代码示例
- Swift UnsafeRawBufferPointer shuffled(using:)用法及代码示例
- Swift UnsafeRawBufferPointer isEmpty用法及代码示例
- Swift UnsafeRawBufferPointer enumerated()用法及代码示例
- Swift UnsafeRawBufferPointer subscript(_:)用法及代码示例
- Swift UnsafeRawBufferPointer starts(with:)用法及代码示例
- Swift UnsafeRawBufferPointer firstIndex(where:)用法及代码示例
- Swift UnsafeRawBufferPointer contains(where:)用法及代码示例
- Swift UnsafeRawBufferPointer first(where:)用法及代码示例
- Swift UnsafeRawBufferPointer allSatisfy(_:)用法及代码示例
- Swift UnsafeRawBufferPointer suffix(_:)用法及代码示例
- Swift UnsafeRawBufferPointer randomElement(using:)用法及代码示例
- Swift UnsafeRawBufferPointer sorted()用法及代码示例
- Swift UnsafeRawBufferPointer indices用法及代码示例
- Swift UnsafeRawBufferPointer compactMap(_:)用法及代码示例
- Swift UnsafeRawBufferPointer init(rebasing:)用法及代码示例
- Swift UnsafeRawBufferPointer elementsEqual(_:)用法及代码示例
- Swift UnsafeRawBufferPointer reduce(into:_:)用法及代码示例
- Swift UnsafeRawBufferPointer filter(_:)用法及代码示例
- Swift UnsafeRawBufferPointer flatMap(_:)用法及代码示例
- Swift UnsafeRawBufferPointer randomElement()用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 UnsafeRawBufferPointer load(fromByteOffset:as:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。