UnsafePointer

struct UnsafePointer<Pointee>

A raw pointer for accessing data of type Pointee. This type provides no automated memory management, and therefore must be handled with great care to ensure safety.

Instances must be aligned to MemoryLayout<Pointee>.alignment, i.e. (UnsafePointer<Int8>(self) - nil) % MemoryLayout<Pointee>.alignment == 0

The memory referenced by an instance can be in one of the following states:

  • Memory is not allocated (for example, pointer is null, or memory has been deallocated previously).

  • Memory is allocated, but value has not been initialized.

  • Memory is allocated and value is initialized.

Inheritance CVarArg, Comparable, CustomDebugStringConvertible, CustomPlaygroundQuickLookable, CustomReflectable, Equatable, Hashable, Strideable View Protocol Hierarchy →
Associated Types
Distance = Int
Import import Swift

Initializers

init(_: OpaquePointer)

Converts from an opaque pointer to a typed pointer.

Declaration

init(_ from: OpaquePointer)
init(_: UnsafeMutablePointer<Pointee>)

Converts from UnsafeMutablePointer to an UnsafePointer of the same Pointee.

Declaration

init(_ other: UnsafeMutablePointer<Pointee>)
init(_: UnsafePointer<Pointee>)

Creates an UnsafePointer from another UnsafePointer.

Declaration

init(_ other: UnsafePointer<Pointee>)
init?(_: OpaquePointer?)

Converts from an opaque pointer to a typed pointer.

Returns nil if from is nil.

Declaration

init?(_ from: OpaquePointer?)
init?(_: UnsafeMutablePointer<Pointee>?)

Converts from UnsafeMutablePointer to an UnsafePointer of the same Pointee.

Returns nil if from is nil.

Declaration

init?(_ other: UnsafeMutablePointer<Pointee>?)
init?(_: UnsafePointer<Pointee>?)

Creates an UnsafePointer from another UnsafePointer.

Returns nil if other is nil.

Declaration

init?(_ other: UnsafePointer<Pointee>?)
init?(bitPattern: Int)

Creates an UnsafePointer with a given pattern of bits.

Returns nil if bitPattern is zero.

Declaration

init?(bitPattern: Int)
init?(bitPattern: UInt)

Creates an UnsafePointer with a given pattern of bits.

Returns nil if bitPattern is zero.

Declaration

init?(bitPattern: UInt)

Instance Variables

var customMirror: Mirror

The custom mirror for this instance.

If this type has value semantics, the mirror should be unaffected by subsequent mutations of the instance.

Declaration

var customMirror: Mirror { get }
var customPlaygroundQuickLook: PlaygroundQuickLook

A custom playground Quick Look for this instance.

If this type has value semantics, the PlaygroundQuickLook instance should be unaffected by subsequent mutations.

Declaration

var customPlaygroundQuickLook: PlaygroundQuickLook { get }
var debugDescription: String

A textual representation of the pointer, suitable for debugging.

Declaration

var debugDescription: String { get }
var hashValue: Int

The pointer's hash value.

The hash value is not guaranteed to be stable across different invocations of the same program. Do not persist the hash value across program runs.

Declaration

var hashValue: Int { get }
var pointee: Pointee

Accesses the Pointee instance referenced by self.

Precondition: the pointee has been initialized with an instance of type Pointee.

Declaration

var pointee: Pointee { get }

Subscripts

subscript(_: Int)

Accesses the pointee at self + i.

Precondition: the pointee at self + i is initialized.

Declaration

subscript(i: Int) -> Pointee { get }

Instance Methods

func advanced(by:)

Returns self + n.

Declaration

func advanced(by n: Int) -> UnsafePointer<Pointee>
func distance(to:)

Returns end - self.

Declaration

func distance(to x: UnsafePointer<Pointee>) -> Int
func predecessor()

Returns the previous consecutive position.

Declaration

func predecessor() -> UnsafePointer<Pointee>
func successor()

Returns the next consecutive position.

Declaration

func successor() -> UnsafePointer<Pointee>
func withMemoryRebound(_:capacity:_:)

Rebinds memory at self to type T with capacity to hold count adjacent T values while executing the body closure.

After executing the closure, rebinds memory back to Pointee.

Precondition: Type 'T' is layout compatible with type 'Pointee'. Precondition: The memory self..<self + count * MemoryLayout<T>.stride is bound to Pointee.

Accessing UnsafePointer<T>.pointee requires that the memory be "bound" to type T. A memory location may only be bound to one type at a time, so accessing the same memory as an unrelated type without first rebinding the memory is undefined. self may not be accessed within the body closure because memory is no longer bound to Pointee while it executes. The closure's UnsafePointer<T> argument must not escape the closure because memory is only temporarily bound to T.

To persistently bind this memory to a different type, first obtain a raw pointer to the memory, then invoke the bindMemory API: UnsafeRawPointer(typedPointer).bindMemory(to:capacity:).

Declaration

func withMemoryRebound<T, Result>(to: T.Type, capacity count: Int, _ body: (UnsafePointer<T>) throws -> Result) rethrows -> Result