//基本指针 UnsafePointer<T> const T * UnsafeMutablePointer T * //集合指针 UnsafeBufferPointer const T * //指向一个连续已知类型区域,可以看成一个集合,并支持集合操作 UnsafeMutableBufferPointer T * //指向一个连续已知类型区域,可以看成一个集合,并支持集合操作 //空指针 UnsafeRawPointer const void * UnsafeMutableRawPointer void * UnsafeRawBufferPointer const void * //指向一个连续未知类型区域 UnsafeMutableRawBufferPointer void * //指向一个连续未知类型区域
//UnsafePointer参数只能接收inout修饰的类型,而inout修饰的类型必然是可写的,所以参数只能用var定义 func call(_ p: UnsafePointer<Int>) { print("\(p.pointee)") } var a = 1234 //&a用于传递指针 call(&a) // 打印:1234
func modify(_ p: UnsafeMutablePointer<Int>) { p.pointee = 5678 } var a = 1234 modify(&a) print("\(a)") // 打印:5678
let a: [Int32] = [1, 2, -1, -2, 5, 6] let p = a.withUnsafeBufferPointer { $0 } print("\(p.count)") // 打印:6 print("\(p[3])") // 打印:-2
let rp: UnsafeMutablePointer<CChar> = rRes.value let strLength = rRes.length //将UnsafeMutablePointer<CChar> C语言的char *指针转成 UnsafeMutableRawPointer 的void *任意指针。然后直接取出封装成String字符串 let strRes = String.init(bytesNoCopy: UnsafeMutableRawPointer(mutating: rp), length: Int(strLength), encoding: .utf8, freeWhenDone: false)
UnsafePointer 基本指针 //将指针的类型转换成一个临时的给定类型,并将指针传递给closure func withMemoryRebound<T, Result>(to: T.Type, capacity: Int, (UnsafePointer<T>) -> Result) -> Result //向下移动一位,并返回一个新的指针 func successor() -> UnsafePointer<Pointee> //向上移动一位,并返回一个新的指针 func predecessor() -> UnsafePointer<Pointee> RawPointer 空指针 //转换给指定类型的指针 func assumingMemoryBound<T>(to: T.Type) -> UnsafeMutablePointer<T> //转换成指定类型的指针,capacity指定了这个指针读取的T数据数量 func bindMemory<T>(to type: T.Type, capacity count: Int) -> UnsafeMutablePointer<T> UnsafeBufferPointer 集合指针 let rpSub:UnsafeMutablePointer<UInt8> = rp.withMemoryRebound(to: UInt8.self, capacity: Int(strLength), { $0 }) let buffer = UnsafeBufferPointer<UInt8>.init(start: rpSub, count: Int(strLength))
//返回一个T类型指针。将第一个参数T以指针的形式传递给closure func withUnsafePointer<T, Result>(to: T, (UnsafePointer<T>) -> Result) -> Result func withUnsafePointer<T, Result>(to: inout T, (UnsafePointer<T>) -> Result) -> Result func withUnsafeMutablePointer<T, Result>(to: inout T, (UnsafeMutablePointer<T>) -> Result) -> Result func withUnsafePointer<T, Result>(to value: inout T, _ body: (UnsafePointer<T>) throws -> Result) rethrows -> Result
var a = 1234 let p = withUnsafePointer(to: &a) { $0 } print("\(p.pointee)") // 打印:1234
var a: UInt32 = 0x12345678 let p = withUnsafeBytes(of: &a) { $0 } var log = "" for item in p { let hex = NSString(format: "%x", item) log += "\(hex)" } print("\(p.count)") // 打印:4 print("\(log)") // 对于小端机器会打印:78563412
let p = UnsafeMutablePointer<Int32>.allocate(capacity: 1) p.initialize(to: 0) // 初始化 p.pointee = 32 print("\(p.pointee)") // 打印:32 p.deinitialize(count: 1) // 反初始化 p.deallocate()
var n = 10086 // malloc let p = malloc(MemoryLayout<Int32>.size)! // memcpy memcpy(p, &n, MemoryLayout<Int32>.size) let p2 = p.assumingMemoryBound(to: Int32.self) print("\(p2.pointee)") // 打印:10086 // strcpy let str = "abc".cString(using: .ascii)! if str.count != MemoryLayout<Int32>.size { return } let pstr = p.assumingMemoryBound(to: CChar.self) strcpy(pstr, str) print("\(String(cString: pstr))") // 打印:abc // strlen print("\(strlen(pstr))") // 打印: 3 // memset memset(p, 0, MemoryLayout<Int32>.size) print("\(p2.pointee)") // 打印:0 // strcat strcat(pstr, "h".cString(using: .ascii)!) strcat(pstr, "i".cString(using: .ascii)!) print("\(String(cString: pstr))") // 打印:hi // strstr let s = strstr(pstr, "i")! print("\(String(cString: s))") // 打印:i // strcmp print("\(strcmp(pstr, "hi".cString(using: .ascii)!))") // 打印:0 // free free(p)
let rRes = c_shmread() let rp: UnsafeMutablePointer<CChar> = rRes.value let strLength = rRes.length //1.先将UnsafeMutablePointer<CChar> C语言的char *指针转成 UnsafeMutableRawPointer 的void *任意指针。然后直接取出封装成String字符串 if let strRes = String.init(bytesNoCopy: UnsafeMutableRawPointer(mutating: rp), length: Int(strLength), encoding: .utf8, freeWhenDone: false) { return strRes } else { return "" } //2.使用withMemoryRebound转换指针类型从CChar转成UInt8类型; let rpSub:UnsafeMutablePointer<UInt8> = rp.withMemoryRebound(to: UInt8.self, capacity: Int(strLength), { $0 }) //然后创建集合类型指针UnsafeBufferPointer; let buffer = UnsafeBufferPointer<UInt8>.init(start: rpSub, count: Int(strLength)) buffer.forEach { print($0) } //然后创建Data, 再将data转成String let data = Data(bytes: rpSub, count: Int(strLength)) let str = String(data: data, encoding: String.Encoding.utf8)