實例方法
bind
bindMemory(to:capacity:)
將內存綁定到指定類型並返回指向綁定內存的類型化指針。
聲明
@discardableResult func bindMemory<T>(
to type: T.Type,
capacity count: Int
) -> UnsafePointer<T>
返回值
指向新綁定內存的類型化指針。此區域中的內存綁定到 T
,但沒有以任何其他方式修改。該區域的字節數為 count * MemoryLayout<T>.stride
。
參數
type
將內存綁定到的類型
T
。count
綁定到類型
T
的內存量,計為T
的實例。
詳述
使用 bindMemory(to:capacity:)
方法將此指針引用的內存綁定到類型 T
。內存必須未初始化或初始化為與 T
布局兼容的類型。如果內存未初始化,綁定到 T
後仍然未初始化。
在此示例中,為指針 bytesPointer
分配了 100 字節的原始內存,然後將前四個字節綁定到 Int8
類型。
let count = 4
let bytesPointer = UnsafeMutableRawPointer.allocate(
byteCount: 100,
alignment: MemoryLayout<Int8>.alignment)
let int8Pointer = bytesPointer.bindMemory(to: Int8.self, capacity: count)
調用 bindMemory(to:capacity:)
後,bytesPointer
引用的內存的前四個字節綁定到 Int8
類型,盡管它們仍未初始化。分配區域的其餘部分是未綁定的原始內存。最終必須釋放所有 100 字節的內存。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift UnsafeRawPointer advanced(by:)用法及代碼示例
- Swift UnsafeRawPointer ...(_:_:)用法及代碼示例
- Swift UnsafeRawPointer ..<(_:)用法及代碼示例
- 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 bindMemory(to:capacity:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。