Home > AI > Uncategorized

Swift指针

 //指针
        var a = UnsafeMutablePointer<GLuint>.allocate(capacity: 10)
        let refer = [GLuint](repeating: 1, count: 20)
        a.initialize(from: refer, count: 10)
        a[2] = 2
        
        print("-a- ", a) //打印内存地址
        
        a.deallocate(capacity: 10)
        a.deinitialize()
        
        print("-a- ", a)//地址一样
        a = UnsafeMutablePointer<GLuint>.allocate(capacity: 5)
        print("-a- ", a)//地址不同
        
        
        let b = [1,2,3]
        print("b", b)
        
        
        // 2
        let c = UnsafeMutablePointer<Int>.allocate(capacity: 1)
        c.pointee = 88
        print("c value", c.pointee)
        
        
        var d = 42
        withUnsafeMutablePointer(to: &d){ $0.pointee *= 2}
        print("d's value is: \(a)") //84
        
        let e = [1, 2, 3, 4, 5, 6]
        e.withUnsafeBufferPointer({ ptr in
            ptr.forEach({ print("\($0)") }) // 1, 2, 3...
        })

/* pointer */
    /* 1 - modify int value */
    var o1: Int = 8
    func increment(ptr: UnsafeMutablePointer<Int>) ->Int {
      ptr.pointee += 1
      return ptr.pointee
    }
    let result1 = increment(ptr: &o1)
    print(result1) //9
    
    
    func increment2(ptr: inout Int){
      ptr += 1
    }
    var o2 = 10
    increment2(ptr: &o2)
    print(o2)
    
    // 2 - 申请了一个 Int 大小的内存,并返回指向这块内存的指针
    let intPtr = UnsafeMutablePointer<Int>.allocate(capacity: 1)
    intPtr.initialize(to: 6)
    print(intPtr.pointee)
    
    let intSwift: Int = 6

    // 3 - UInt8 byte
    let ascii: Array<UInt8> = [115, 116, 114, 105, 110, 103, 48, 49, 50]
    let aData = Data(bytes: ascii)
    let aString = String(data: aData, encoding: String.Encoding.utf8)
    print(aString) //string012
    
    
    var o3 = [1, 2, 3, 4, 5]
    let o3mb = UnsafeMutableBufferPointer<Int>(start: &o3, count: o3.count)
    
    var o4 = UnsafeMutablePointer<GLuint>.allocate(capacity: 10)
    let refer = [GLuint](repeating: 1, count: 20)
    o4.initialize(from: refer, count: 10)
    o4[2] = 2
    
    print("-a- ", o4) //打印内存地址
    
    o4.deallocate(capacity: 10)
    o4.deinitialize()
    
    print("-a- ", o4)//地址一样
    o4 = UnsafeMutablePointer<GLuint>.allocate(capacity: 5)
    print("-a- ", o4)//地址不同

func convert(data: UnsafePointer<Int8>, length: Int)  -> [Int8] {
      let buffer = UnsafeBufferPointer(start: data, count: length)
      return Array(buffer)
    }
 
    let a = UnsafePointer<Int8>.init(bitPattern: 0) // = nil
    
    //let result = convert(data: a!, length: 10)
    //print("result", result)
    
    //UInt8  = [0, 255]         //2^8
    //Uint16 = [0, 65535]       //2^16
    //UInt32 = [0, 4294967295]  //2^32
    //0xFFFF_FFFF = 4294967295
    let b = UnsafeMutableRawPointer.allocate(byteCount: 4, alignment: 1)
    b.storeBytes(of: 0xFFFF_FFFF, as: UInt32.self)

    let c = b.load(as: UInt8.self) //1st byte
    
    let offset = b + 2
    let y = offset.load(as: UInt16.self)
    b.deallocate()
    
    let emptyBuffer = UnsafeBufferPointer<UInt8>(start: nil, count: 0)

    
    let BUFFER_SIZE = 2048
    let buf = NSMutableData(capacity: BUFFER_SIZE)
    print(buf?.bytes) //UnsafeRawPointer
    print(buf)

    
    let buffer = UnsafeMutableRawPointer(mutating: buf?.bytes )?.assumingMemoryBound(to: UInt8.self)
    print(buffer)

//指针了解短篇
    //UnsafeMutablePointer -----------> UnsafePointer ----------------------------------------------> Array
    //                      指针类型转换                 UnsafeBufferPointer / withUnsafeBufferPointer
    //                     <-----------               <----------------------------------------------
    //UnsafeRawPointer 数据类型转换(assumingMemoryBound)
    
    
    let mp = UnsafeMutablePointer<Int>.allocate(capacity: 2)
    mp.initialize(to: 3)
    let p = UnsafePointer<Int>.init(mp)
    print(mp)                    //0x00000001c400fec0
    print(p)                     //0x00000001c400fec0 内存地址
    print(p.pointee)             //3 此时指针指向的内容
    
    var mp2 = mp.successor()
    mp2.pointee = 10             //UnsafeMutablePointer可以改,UnsafePointer则只读
    print(mp2.pointee)           //10
    print(p.successor().pointee) //10 跟着mp变
    
    //UnsafePinter 转 array
    let buffer = UnsafeBufferPointer(start: p, count: 2)
    print(buffer.baseAddress)                           //0x00000001c400fec0
    let result: [Int] = Array(buffer)
    print("result", result)                             //[Int] [3, 10]
    let intSize = MemoryLayout<Int>.size                //8
    let resultSize = MemoryLayout.size(ofValue: result) //8
    if resultSize == intSize {
      print("result 类型是int")
    }
    
    //数据类型转换
    let rawPtr = UnsafeRawPointer(p)
    let dataTypeTurned = rawPtr.assumingMemoryBound(to: UInt32.self)
    let temp = UnsafeBufferPointer(start: dataTypeTurned, count: 2)
    let final = Array(temp)
    print("final", final)                               //[UInt32] [3, 0]

 

Related posts:

Leave a Reply