指针

作者: 曹来东 | 来源:发表于2019-08-03 17:33 被阅读0次
    • Swift中也有专门的指针类型,这些都被定性为Unsafe不安全的,常见的又一下四种类型
      1.UnsafePonter<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
        print(ptr.pointee)
    }
    
    func test2(_ ptr: UnsafePointer<Int>) {
        print(ptr.pointee)
    }
    test1(&age)//20
    test2(&age)//20
    print(age)//20
    
    var age = 10
    func test3(_ ptr: UnsafeMutableRawPointer) {
        ptr.storeBytes(of: 20, as: Int.self)
    }
    
    func test4(_ ptr: UnsafeRawPointer) {
        print(ptr.load(as: Int.self))
    }
    test3(&age)
    test4(&age)//20
    print(age)//20
    
    • 指针的应用示例
    import Foundation
    
    var arr = NSArray(objects: 11,22,33,44)
    arr.enumerateObjects { (obj, idx, stop) in
        print(idx,obj)
        
        if idx == 2 {//下标为2就停止遍历
            stop.pointee = true
        }
    }
    /* 打印结果
     0 11
     1 22
     2 33
     */
    var arr1 = NSArray(objects: 11,22,33,44)
    for (idx, obj) in arr1.enumerated() {
        print(idx,obj)
        if idx == 2 {
            break
        }
    }
    /* 打印结果
     0 11
     1 22
     2 33
     */
    
    • 获得指向某个变量的指针
    var age = 11
    var ptr1 = withUnsafeMutablePointer(to: &age) { $0 }
    
    var ptr2 = withUnsafePointer(to: &age, { $0 })
    
    ptr1.pointee = 22
    print(ptr2.pointee)//2
    print(age)//22
    
    var ptr3 = withUnsafeMutablePointer(to: &age) { UnsafeMutableRawPointer($0)}
    
    var ptr4 = withUnsafePointer(to: &age) { UnsafeRawPointer($0)
    }
    
    ptr3.storeBytes(of: 33, as: Int.self)
    print(ptr4.load(as: Int.self))//33
    print(age)//33
    
    • 获得指向堆空间实例的指针
    class Person { }
    var person = Person()
    var ptr = withUnsafePointer(to: &person) { UnsafeRawPointer($0)
    }
    
    var heapPtr = UnsafeRawPointer(bitPattern: ptr.load(as: UInt.self))
    print(ptr)//0x00000001000021c0 全局区的指针
    print(heapPtr!)//0x0000000100508610 堆区指针
    
    • 创建指针
    import Foundation
    var ptr = UnsafeRawPointer(bitPattern: 0x0000000100508610)
    
    //创建
    var ptr = malloc(16)
    //存
    ptr?.storeBytes(of: 11, as: Int.self)
    ptr?.storeBytes(of: 22, toByteOffset: 8, as: Int.self)
    //取
    print((ptr?.load(as: Int.self))!)//11
    print((ptr?.load(fromByteOffset: 8, as: Int.self))!)//22
    //销毁
    free(ptr)
    
    var ptr = UnsafeMutableRawPointer.allocate(byteCount: 16, alignment: 1)
    ptr.storeBytes(of: 11, as: Int.self)
    ptr.advanced(by: 8).storeBytes(of: 22, as: Int.self)
    print(ptr.load(as: Int.self))//11
    print(ptr.advanced(by: 8).load(as: Int.self))//22
    ptr.deallocate()
    
    var ptr = UnsafeMutablePointer<Int>.allocate(capacity: 3)
    ptr.initialize(to: 11)
    ptr.successor().initialize(to: 32)
    ptr.successor().successor().initialize(to: 33)
    print(ptr.pointee)//11
    print((ptr + 1).pointee)//22
    print((ptr + 2).pointee)//33
    print(ptr[0])//11
    print(ptr[1])//22
    print(ptr[2])//33
    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: 10, name: "Jack"))
    (ptr + 1).initialize(to: Person(age: 11, name: "Rose"))
    (ptr + 2).initialize(to: Person(age: 12, name: "Kate"))
    //Jack deinit
    //Rose deinit
    //Kate deinit
    ptr.deinitialize(count: 3)
    ptr.deallocate()
    
    • 指针之间的转换
    var ptr = UnsafeMutableRawPointer.allocate(byteCount: 16, alignment: 1)
    ptr.assumingMemoryBound(to: Int.self).pointee = 11
    (ptr + 8).assumingMemoryBound(to: Double.self).pointee = 22.0
    print(unsafeBitCast(ptr, to: UnsafePointer<Int>.self).pointee)//11
    print(unsafeBitCast(ptr + 8, to: UnsafePointer<Double>.self).pointee)//22.0
    
    ptr.deallocate()
    
    • unsafeBitCast是忽略数据类型的强制转换,不会因为数据类型的变化而改编原来的内存数据
    • 类似C++中的reinterpret_cast
    class Person { }
    var person = Person()
    var ptr = unsafeBitCast(person, to: UnsafeRawPointer.self)
    print(ptr)//0x0000000100508b80
    

    相关文章

      网友评论

          本文标题:指针

          本文链接:https://www.haomeiwen.com/subject/nyajdctx.html