指针类型
是不安全的
1.UnsafePointer<Pointee> 类似于const Pointee * 只可以用来访问内存 不可以修改
2.UnsafeMutablePointer<Pointee> 类似于Pointee * 可以用来访问内存 可以修改
3.UnsafeRawPointer 类似于const void * 不支持泛型
4.UnsafeMutableRawPointer 类似于void * 不支持泛型
var age = 10
func test1(_ ptr: UnsafeMutablePointer<Int>){
ptr.pointee += 10
}
func test2(_ ptr: UnsafePointer<Int>){
print(ptr.pointee)
//ptr.pointee 取出指针指向的数据
}
test1(&age)
test2(&age)
print(age)
20
20
ptr.pointee 取出指针指向的数据
func test3(_ ptr: UnsafeRawPointer){
let d = ptr.load(as: Int.self)
print("test3",d)
}
func test4(_ ptr: UnsafeMutableRawPointer){
//不支持泛型 那么就要自己设置是什么类型 传入什么值
ptr.storeBytes(of: 30, as: Int.self)
}
test3(&age)
test4(&age)
print(age)
ptr.load(as: Int.self) 指针指向的Int数据
ptr.storeBytes(of: 30, as: Int.self) 把数据存储到指针指向的空间
指针的应用
var arr = NSArray(objects: 11,22,33,44)
arr.enumerateObjects { (element, idx, stop) in
print(idx,element)
if idx == 2 {
stop.pointee = true
}
}
stop.pointee 代表执行完大括号之后再判断条件是否满足 是否继续执行
可读行更好
var arr = NSArray(objects: 11,22,33,44)
for (idx,element) in arr.enumerated() {
print(idx,element)
if idx == 2 {
break
}
}
获得指向某个变量的指针
var age = 10
var ptr = withUnsafePointer(to: &age) { $0 }
var ptr2 = withUnsafeMutablePointer(to: &age) { $0
}
ptr2.pointee = 30
print(age)
var age = 10
var ptr = withUnsafePointer(to: &age) { UnsafeRawPointer($0)
}
var ptr2 = withUnsafeMutablePointer(to: &age) { UnsafeMutableRawPointer($0)
}
ptr2.storeBytes(of: 30, as: Int.self)
print(ptr.load(as: Int.self))
print(age)
获得指向堆空间实例的指针
class Person {
var age: Int
init(age: Int) {
self.age = age
}
}
var person = Person(age: 21)
var ptr = withUnsafePointer(to: &person) { UnsafeRawPointer($0)
}
//ptr 指向person这个指针变量 存放person这个指针变量的地址值
//ptr.pointee 等价于person
print(ptr)
var ptr2 = UnsafeMutableRawPointer(bitPattern: ptr.load(as: UInt.self))
print(ptr2 ?? 2)
//ptr2 堆空间地址值 Person的地址值
创建指针
var ptr = malloc(16)
//往堆空间里面放入数据
ptr?.storeBytes(of: 10, as: Int.self)
ptr?.storeBytes(of: 20, toByteOffset: 8, as: Int.self)
print(ptr!)
free(ptr)
var ptr = UnsafeMutableRawPointer.allocate(byteCount: 16, alignment: 1)
//指针偏移 8个字节
ptr.advanced(by: 8)
//把22存放在后8个字节
ptr.advanced(by: 8).storeBytes(of: 22, as: Int.self)
//释放
ptr.deallocate()
//capacity 是容量 相当于Int的8个字节 * 3 = 24个字节
var ptr = UnsafeMutablePointer<Int>.allocate(capacity: 2)
ptr.pointee = 10
ptr.advanced(by: 8).pointee = 20
//初始化内存 用10初始化前8个字节
ptr.initialize(to: 10)
//连续的是2个8个字节的10 初始化内存
ptr.initialize(repeating: 10, count: 2)
//successor 后记 跳8个字节(如果是Int) 下一个8个字节初始化10
ptr.successor().initialize(to: 10)
//取出下一个字节的内容
print(ptr.successor().pointee)
print((ptr + 1).pointee)
print(ptr[0])
print(ptr[1])
//反初始化 防止内存泄露
ptr.deinitialize(count: 3)
//释放
ptr.deallocate()
class Person {
var age: Int
var name: String
init(age: Int, name: String) {
self.age = age
self.name = name
}
deinit {
print(name,"deinit")
}
}
var ptr = UnsafeMutablePointer<Person>.allocate(capacity: 3)
ptr.initialize(to: Person(age: 20, name: "jack"))
(ptr + 1).initialize(to: Person(age: 21, name: "Rose"))
(ptr + 2).initialize(to: Person(age: 22, name: "Kate"))
//ptr[2].name
ptr.deinitialize(count: 3)
ptr.deallocate()
初始化多少内存 就要反初始化多少内存 否则 内存泄露
指针之间的转换
var ptr = UnsafeMutableRawPointer.allocate(byteCount: 16, alignment: 1)
//转换为UnsafeMutablePointer指针
var ptr2 = ptr.assumingMemoryBound(to: Int.self)
//ptr2 是泛型的 ptr2 + 1 就是移动8个字节
var ptr4 = ptr2 + 1
//ptr 不是泛型的 就是移动8个字节
var ptr3 = (ptr + 8).assumingMemoryBound(to: Int.self)
//强制转换
var ptr = UnsafeMutableRawPointer.allocate(byteCount: 16, alignment: 1)
var ptr5 = unsafeBitCast(ptr, to: Int.self)
unsafeBitCast 是忽略数据类型的强制转换 不会因为数据类型的变化而改变原来的内存数据
unsafeBitCast
var person = Person()
//把person里面的内容放入ptr 内存数据是一样的 类型不同
var ptr = unsafeBitCast(person, to: UnsafeRawPointer.self)
print(ptr)