Swift超詳細(xì)講解指針
Swift指針Unsafe Pointer
如果不是只讀,可以修改 ( 寫入 ),就加一個(gè) Mutable,
如果沒(méi)有具體的類型( 通過(guò)泛型的方式 ),就加一個(gè) Raw,
如果不是一個(gè)單獨(dú)的對(duì)象 ( 指向集合類型 ),就加上 buffer.
Unsafe [ Mutable ] [ Raw ] [ Buffer ] Pointer [ ]
蘋果沒(méi)有編譯保護(hù)的 [ 可變的 ] [沒(méi)有類型的] [ 是集合的 ] 指針 [< 具體的類型 >]
對(duì)照Objective-C
- swift 的
unsafeMutablePointer<T>
: OC 的 T * - swift 的
unsafePointer<T>
: OC 的 const T * - swift 的 unsafeRawPointer: OC 的 const void *
- swift 的 unsafeMutableRawPointer: OC 的 void *
例子
例子 1, 無(wú)類型的指針
let count = 2 let stride = MemoryLayout<Int>.stride let alignment = MemoryLayout<Int>.alignment let byteCount = stride * count do { print("Raw pointers") let pointer = UnsafeMutableRawPointer.allocate( byteCount: byteCount, alignment: alignment) // 指針的創(chuàng)建,與銷毀 defer { // 需要手動(dòng)管理,指針的內(nèi)存 pointer.deallocate() } // store 存值 pointer.storeBytes(of: 42, as: Int.self) // 指針需要移動(dòng) stride,才能到達(dá)下一個(gè)指針 pointer.advanced(by: stride).storeBytes(of: 6, as: Int.self) // (pointer+stride).storeBytes(of: 6, as: Int.self), 這個(gè)是另一種方式 // load 取值 print(pointer.load(as: Int.self)) print(pointer.advanced(by: stride).load(as: Int.self)) // 集合的指針 let bufferPointer = UnsafeRawBufferPointer(start: pointer, count: byteCount) for (index, byte) in bufferPointer.enumerated() { print("byte \(index): \(byte)") } }
2, 具體類型的指針
具體類型的指針,可以通過(guò)指針的 pointee
屬性,方便的操作 load 和 store
let count = 2 let stride = MemoryLayout<Int>.stride let alignment = MemoryLayout<Int>.alignment let byteCount = stride * count do { print("Typed pointers") let pointer = UnsafeMutablePointer<Int>.allocate(capacity: count) pointer.initialize(repeating: 0, count: count) // 與上面的一樣,指針的內(nèi)存,需要手動(dòng)管理 defer { pointer.deinitialize(count: count) pointer.deallocate() } pointer.pointee = 42 // 因?yàn)榫幾g器做了優(yōu)化,指針到達(dá)下一個(gè)指針,不需要移動(dòng) stride // 指針移動(dòng) 1 ,就到了下一個(gè)指針 pointer.advanced(by: 1).pointee = 6 print( pointer.pointee ) print(pointer.advanced(by: 1).pointee) let bufferPointer = UnsafeBufferPointer(start: pointer, count: count) for (index, value) in bufferPointer.enumerated() { print("value \(index): \(value)") } }
例子 3: 通過(guò)綁定內(nèi)存,來(lái)做指針的轉(zhuǎn)化
bindMemory
let count = 2 let stride = MemoryLayout<Int>.stride let alignment = MemoryLayout<Int>.alignment let byteCount = stride * count do { print("Converting raw pointers to typed pointers") let rawPointer = UnsafeMutableRawPointer.allocate( byteCount: byteCount, alignment: alignment) defer { rawPointer.deallocate() } // 這一步,將任意指針,轉(zhuǎn)化為類型指針 let typedPointer = rawPointer.bindMemory(to: Int.self, capacity: count) typedPointer.initialize(repeating: 0, count: count) defer { typedPointer.deinitialize(count: count) } typedPointer.pointee = 42 typedPointer.advanced(by: 1).pointee = 6 // 看結(jié)果 print(typedPointer.pointee) print(typedPointer.advanced(by: 1).pointee) let bufferPointer = UnsafeBufferPointer(start: typedPointer, count: count) for (index, value) in bufferPointer.enumerated() { print("value \(index): \(value)") } }
例子 4, 查看指針的字節(jié)
struct Demo{ let number: UInt32 let flag: Bool } do { print("Getting the bytes of an instance") var one = Demo(number: 25, flag: true) withUnsafeBytes(of: &one) { bytes in for byte in bytes { print(byte) } } }
例子 4.1, 指針的字節(jié), 算 check sum
struct Demo{ let number: UInt32 let flag: Bool } do { print("Checksum the bytes of a struct") var one = Demo(number: 25, flag: true) let checksum = withUnsafeBytes(of: &one) { (bytes) -> UInt32 in return ~bytes.reduce(UInt32(0)) { $0 + numericCast($1) } } print("checksum", checksum) // checksum 4294967269 }
checeSum 的使用,分為 checeSum 的計(jì)算與校驗(yàn)
本文簡(jiǎn)單描述 checeSum 的計(jì)算
數(shù)據(jù)塊,分為 n 個(gè)包, size 相同
拿包的字節(jié),計(jì)算 checkSum, checkSum 的大小限制在包的 size
例子 5, 獲取變量的指針
var cat = "fly" // 返回的是,閉包中的參數(shù) // withUnsafePointer , 把閉包里面的結(jié)果,rethrow 出去 ( 相當(dāng)于 return 出來(lái) ) let warrior = withUnsafePointer(to: &cat, { $0 }) print(warrior.pointee)
例子 6, 指向多個(gè)元素的指針
struct Cat{ var habit = "eat" var paws = 6 var name = "load" } let ptr = UnsafeMutablePointer<Cat>.allocate(capacity: 2) // 指向兩個(gè) Cat 結(jié)構(gòu)體 ptr.initialize(repeating: Cat(), count: 2) defer{ ptr.deinitialize(count: 2) ptr.deallocate() } var one = Cat() one.paws = 8 ptr[1] = one // 以下兩個(gè)等價(jià) print(ptr[0]) print(ptr.pointee) // 下面 3 個(gè)等價(jià) print(ptr[1]) print((ptr + 1).pointee) print(ptr.successor().pointee)
例子 7: 元素組合的探索
var pair = (66, 666) func test(ptr: UnsafePointer<Int>){ print(ptr.pointee) print(ptr.successor().pointee) } withUnsafePointer(to: &pair) { (tuplePtr: UnsafePointer<(Int, Int)>) in // 假定內(nèi)存綁定,不需要經(jīng)過(guò)內(nèi)存檢查 test(ptr: UnsafeRawPointer(tuplePtr).assumingMemoryBound(to:Int.self)) }
參考了 Unsafe Swift: Using Pointers and Interacting With C
到此這篇關(guān)于Swift超詳細(xì)講解指針的文章就介紹到這了,更多相關(guān)Swift指針內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Swift的74個(gè)常用內(nèi)置函數(shù)介紹
這篇文章主要介紹了Swift的74個(gè)常用內(nèi)置函數(shù)介紹,這篇文章列舉出了所有的Swift庫(kù)函數(shù),內(nèi)置函數(shù)是指無(wú)需引入任何模塊即可以直接使用的函數(shù),需要的朋友可以參考下2015-01-01利用Swift實(shí)現(xiàn)一個(gè)響應(yīng)式編程庫(kù)
最近在學(xué)習(xí)swift,最近有空所以總結(jié)一下最近學(xué)習(xí)的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于利用Swift實(shí)現(xiàn)一個(gè)響應(yīng)式編程庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-12-12Swift中defer關(guān)鍵字推遲執(zhí)行示例詳解
這篇文章主要給大家介紹了關(guān)于Swift中defer關(guān)鍵字推遲執(zhí)行的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03淺談在Swift中關(guān)于函數(shù)指針的實(shí)現(xiàn)
這篇文章主要介紹了淺談在Swift中關(guān)于函數(shù)指針的實(shí)現(xiàn),是作者根據(jù)C語(yǔ)言的指針特性在Swifft中做出的一個(gè)實(shí)驗(yàn),需要的朋友可以參考下2015-07-07在 Swift 中測(cè)試 UIAlertController的方法
這篇文章主要介紹了在 Swift 中測(cè)試 UIAlertController的方法的,需要的朋友可以參考下2015-10-10Swift實(shí)現(xiàn)Selection Sort選擇排序算法的實(shí)例講解
選擇排序是一種穩(wěn)定的排序算法,且實(shí)現(xiàn)代碼通常比冒泡排序要來(lái)的簡(jiǎn)單,這里我們就來(lái)看一下Swift實(shí)現(xiàn)Selection Sort選擇排序的實(shí)例講解2016-07-07