struct iPhone{
var memorySize: Int
var weight: Double
}
/*
1.指定类型的指针 type pointer
2.未指定类型的指针 即:原生指针 raw pointer
UnsafePointer:指针以及指向内容不可变
UnsafeMutablePointer: 指针以及指向内容都可变
UnsafeRawPointer: 指针指向的内存区域未定
UnsafeMutableRawPointer: 指针指向的内存区域未定
UnsafeBufferPointer: 连续的内存空间
UnsafeMutableBufferPointer
UnsafeRawBufferPointer
UnsafeMutableRawBufferPointer
*/
// 利用原始指针存储4个整型数据
static func rawPointer(){
let p = UnsafeMutableRawPointer.allocate(byteCount: 32, alignment: 8)
for i in 0..<4{
// p.storeBytes(of: i, as: Int.self) 不好使\
//size 是 struct结构体的大小, stride是占有内存的实际大小,alignment对应的是stride
//存储数据 注意类型是.self
p.advanced(by: i * MemoryLayout<Int>.stride).storeBytes(of: i, as: Int.self)
}
for i in 0..<4{
let value = p.load(fromByteOffset: i * 8, as: Int.self)
print(value)
}
p.deallocate()
}
/** 运行结果
0
1
2
3
*/
//泛型指针(不可变)
static func unsafePointer(){
var age = 18
//获取
withUnsafePointer(to: &age) { pt in
//打印指针
print(pt)
}
//新地址
age = withUnsafePointer(to: &age, { ptr in
//具体内容+21
ptr.pointee + 21
})
print( age
)
}
/** 运行结果
0x000000016d34d0b0
39
*/
//泛型指针(可变)
static func unsafeMuteblePointer(){
//必须可变
var age = 10
//改变原地址存储的值。⚠️是to
withUnsafeMutablePointer(to: &age) { ptr in
ptr.pointee = ptr.pointee + 21
}
print(age)
}
/** 运行结果
31
*/
// 直接内存地址分配
static func memoryAlloc_1(){
var age = 10
let ptr = UnsafeMutablePointer<Int>.allocate(capacity: 1)
ptr.initialize(to: age)
print(ptr.pointee)
}
/** 运行结果
10
*/
//初始化指针内存
static func memoryAlloc_2(){
var ptr = UnsafeMutablePointer<iPhone>.allocate(capacity: 5)
//存入指针数据
ptr[0] = iPhone.init(memorySize: 3, weight: 1)
ptr[1] = iPhone.init(memorySize: 3, weight: 2)
//完成
ptr.deinitialize(count: 5)
//这里可以拿到数据
print(ptr[1])
print(ptr[2])
//销毁
ptr.deallocate()
//这里已经拿不到数据了
// print(ptr[0])
}
/** 运行结果
iPhone(memorySize: 3, weight: 2.0)
iPhone(memorySize: 0, weight: 0.0)
*/
//这个没看懂
static func memoryAlloc_3(){
let count = 3
var ptr = UnsafeMutablePointer<iPhone>.allocate(capacity: count)
ptr.initialize(to: iPhone(memorySize: 5, weight: 6))
ptr.advanced(by: MemoryLayout<iPhone>.stride).initialize(to: iPhone(memorySize: 6, weight: 7))
ptr.deinitialize(count: count)
print(ptr[0])
ptr.deallocate()
}
/** 运行结果
iPhone(memorySize: 5, weight: 6.0)
*/
static func testPoint(_ p: UnsafePointer<Int>){
print(p[0])
print(p[1])
}
//内存绑定 assumingMemoryBound 告诉编译器传递的参数类型 这样不会报错
static func assumingMemory(){
let tuple = (10,20)
withUnsafePointer(to: tuple) { (ptr:UnsafePointer<(Int,Int)>) in
testPoint(UnsafeRawPointer(ptr).assumingMemoryBound(to: Int.self))
}
}
/** 运行结果
10
20
*/
static func testPoint2(_ p: UnsafePointer<Int8>){
print(p)
}
//更改内存绑定 发生了实际类型的转换
static func bindMemory(){
let tuple = (10,20)
withUnsafePointer(to: tuple) { (ptr:UnsafePointer<(Int,Int)>) in
testPoint(UnsafeRawPointer(ptr).bindMemory(to: Int.self, capacity: 1))
}
}
/** 运行结果
10
20
*/
//临时更改内存来绑定类型
static func reboundMemory(){
let uint8Ptr = UnsafePointer<UInt8>.init(bitPattern: 10)
uint8Ptr?.withMemoryRebound(to: Int8.self, capacity: 1, { (pointer: UnsafePointer<Int8>) in
testPoint2(pointer)
})
}
/** 运行结果
0x000000000000000a
*/