swift Pointer 的使用

Pointer Name Unsafe? Write Access? Collection Strideable? Typed?
UnsafeMutablePointer yes yes no yes yes
UnsafePointer yes no no yes yes
UnsafeMutableBufferPointer yes yes yes no yes
UnsafeBufferPointer yes no yes no yes
UnsafeRawPointer yes no no yes no
UnsafeMutableRawPointer yes yes no yes no
UnsafeMutableRawBufferPointer yes yes yes no no
UnsafeRawBufferPointer yes no yes no no
  • unsafe:不安全的
  • Write Access:可写入
  • Collection:像一个容器,可添加数据
  • Strideable:指针可使用 advanced 函数移动
  • Typed:是否需要指定类型(范型)

MemoryLayout
使用MemoryLayout,可以检测某个类型的实际大小(size),内存对齐大小(alignment),以及实际占用的内存大小(步长:stride),其单位均为字节;

public enum MemoryLayout {
    public static var size: Int { get }
    public static var stride: Int { get }
    public static var alignment: Int { get }
    public static func size(ofValue value: T) -> Int
    public static func stride(ofValue value: T) -> Int
    public static func alignment(ofValue value: T) -> Int
}

你可能感兴趣的:(swift Pointer 的使用)