指針?lè)诸?lèi)
使用swift提供指針類(lèi)型:
- UnsafePointer
- UnsafeMutablePointer
- UnsafeRawPointer
- UnsafeMutableRawPointer
UnsafePointer / UnsafeMutablePointer用法:
使用 pointee 屬性可以進(jìn)行讀寫(xiě)操作;
var age = 10
func change1(_ ptr: UnsafeMutablePointer<Int>) {
ptr.pointee += 20
}
func change2(_ ptr: UnsafePointer<Int>) {
print(ptr.pointee)
}
change1(&age)
change2(&age) // 30
print(age) // 30
UnsafeRawPointer / UnsafeMutableRawPointer
var age = 10
func change3(_ ptr: UnsafeMutableRawPointer) {
ptr.storeBytes(of: 20, as: Int.self)
}
func change4(_ ptr: UnsafeRawPointer) {
print(ptr.load(as: Int.self))
}
change3(&age)
change4(&age) // 20
print(age) // 20
獲取堆空間地址
class Person {}
var person = Person()
//獲取person指針的地址值
var ptr = withUnsafePointer(to: &person) { UnsafeRawPointer($0) }
// 獲取person指針的指向的地址,就是為Person的堆空間地址
var headPtr = UnsafeRawPointer(bitPattern: ptr.load(as: UInt.self))
print(ptr)
print(headPtr)
print(person)
0x00000001000031f8
0x00000001006b6340
- withUnsafePointer 的閉包返回類(lèi)型就是person變量指針的地址值(棧空間);
- headPtr 是獲取 ptr指向的地址值流妻,就是Person實(shí)例對(duì)象的地址(堆空間)蹲堂;
創(chuàng)建 UnsafeRawPointer / UnsafeMutableRawPointer 創(chuàng)建指針
- 方式一:通過(guò)地址值創(chuàng)建
var ptr = UnsafeRawPointer(bitPattern: 0x0000000100003008)
- 方式二:
var ptr = malloc(16) // UnsafeMutableRawPointer
ptr?.storeBytes(of: 10, as: Int.self) // 8字節(jié)
ptr?.storeBytes(of: 20, toByteOffset: 8, as: Int.self) // 存放在后8字節(jié)中
print( (ptr?.load(as: Int.self))! ) // 10
print( (ptr?.load(fromByteOffset: 8, as: Int.self))! ) // 20
free(ptr)
- 方式三:
var ptr = UnsafeMutableRawPointer.allocate(byteCount: 16, alignment: 1)
ptr.storeBytes(of: 10, as: Int.self)
//ptr.advanced(by: 8).storeBytes(of: 20, as: Int.self)
(ptr + 8).storeBytes(of: 20, as: Int.self)
print( ptr.load(as: Int.self) ) // 10
print( ptr.load(fromByteOffset: 8, as: Int.self) ) // 20
print( ptr.advanced(by: 8).load(as: Int.self) ) // 20
print( (ptr + 8).load(as: Int.self) ) //20
ptr.deallocate()
fromByteOffset 與 advanced都能讓指針進(jìn)行偏移纬向;創(chuàng)建指針后燕偶,記得釋放;
UnsafePointer / UnsafeMutablePointer 創(chuàng)建指針
var ptr = UnsafeMutablePointer<Int>.allocate(capacity: 3)
ptr.initialize(to: 10)
ptr.successor().initialize(to: 20)
ptr.successor().successor().initialize(to: 30)
print(ptr.pointee) // 10
print(ptr.successor().pointee) // 20
print(ptr.successor().successor().pointee) // 30
ptr.deinitialize(count: 3)
ptr.deallocate()
或者:
var ptr = UnsafeMutablePointer<Int>.allocate(capacity: 3)
ptr.initialize(to: 10)
(ptr + 1).initialize(to: 20)
(ptr + 2).initialize(to: 30)
print(ptr.pointee) // 10
print((ptr + 1).pointee) // 20
print((ptr + 2).pointee) // 30
ptr.deinitialize(count: 3)
ptr.deallocate()
ptr表示創(chuàng)建3個(gè)整形的指針汰现;initialize為初始化;successor表示每次偏移8字節(jié)(整形)叔壤;
指針之間的轉(zhuǎn)換
var ptr = UnsafeMutableRawPointer.allocate(byteCount: 16, alignment: 1)
// assumingMemoryBound 返回 UnsafeMutablePointer<T>類(lèi)型
ptr.assumingMemoryBound(to: Int.self).pointee = 10
(ptr + 8).assumingMemoryBound(to: Int.self).pointee = 20
print( unsafeBitCast(ptr, to: UnsafePointer<Int>.self).pointee ) // 10
print( unsafeBitCast(ptr + 8, to: UnsafePointer<Int>.self).pointee ) // 20
ptr.deallocate()
- unsafeBitCast 忽略數(shù)據(jù)類(lèi)型的強(qiáng)制轉(zhuǎn)換瞎饲,不會(huì)因?yàn)閿?shù)據(jù)類(lèi)型的變化而改變?cè)瓉?lái)的內(nèi)存結(jié)構(gòu);
總結(jié)
- 以上都是使用整形存儲(chǔ)在指針中炼绘,實(shí)際上存儲(chǔ)對(duì)象嗅战,結(jié)構(gòu)體等;
- UnsafeMutableRawPointer指針+1表示一個(gè)字節(jié)俺亮;UnsafeMutablePointer+1表示泛型對(duì)象占用內(nèi)存字節(jié)數(shù)+1驮捍,即下一個(gè)對(duì)象;
源代碼demo