iOS原理之Block的访问auto变量(对象类型)的实现原理

//
//  BlockBasementCopyViewController.swift
//  iOS底层
//
//  Created by yanqunchao on 2019/5/19.
//  Copyright © 2019 yanqunchao. All rights reserved.
//

import UIKit

class BlockBasementCopyViewController: UIViewController {
    typealias FuncPt = ((MainBlockImpl0)->())
    
    typealias CopyFuncPt = ((MainBlockImpl0,MainBlockImpl0)->())
    
    typealias DisposeFuncPt = ((MainBlockImpl0)->())
    
    
    class BlockImpl: NSObject {
        var isa:UnsafeRawPointer?
        var Flags:Int?
        var Reserved:Int?
        var FuncPtr:FuncPt? //函数地址,
    }
    
    /*
     * 访问auto局部(对象类型)的block类
     **/
    
    class MainBlockDesc0: NSObject {
        var reserved:Int?
        var BlockSize:Int?
        var copy:CopyFuncPt?
        var dispose:DisposeFuncPt?
        
        init(reserved:Int,BlockSize:Int,copy:@escaping CopyFuncPt,dipose:@escaping DisposeFuncPt) {
            self.reserved = reserved
            self.BlockSize = BlockSize
            self.copy = copy
            self.dispose = dipose
        }
        
        override init() {
            
        }
        
    }
  

    /*
     * 访问auto局部变量(对象类型)的block类
     **/
    
    class MainBlockImpl0: NSObject {
        var imp: BlockImpl?
        var Desc: MainBlockDesc0?
        var person: MJPerson? //局部变量的地址 强引用
        //        var weakPerson: MJPerson? //弱引用
        init(fp:@escaping FuncPt,desc:MainBlockDesc0,flag:Int, _person:MJPerson) {
            imp = BlockImpl()
//            imp!.isa = &_NSConcreateStackBlock
            imp?.Flags = 0
            imp?.FuncPtr = fp
            Desc = desc
            person = _person
            //            weakPerson = _person
        }
        override init() {
            
        }
    }
    
    /* 
     *block调用的函数地址
     **/
    
    func mainBlockFunc0(block:MainBlockImpl0){
        print("调用mainBlockFunc0函数获取的变量值为\(String(describing: block.person?.age!))))")
    }
    
    /*
     *当block调用copy操作的时候,就会调用此函数
     **/
    func mainBlockCopy0(destBlock:MainBlockImpl0,srcBlock:MainBlockImpl0){
        blockObjectAssign(destObjc: destBlock.person!, srcObjc: srcBlock.person!, type: 3)
    }
    /*
     * 依据传入的srcObjc的类型,决定是否对其强引用.
     * 如果是strong,就会对其进行强引用(类似于retainCount + 1 的操作),如果是weak,就会对其弱引用。
     **/
    func blockObjectAssign(destObjc:AnyObject,srcObjc:AnyObject,type:Int){
        //            if srcObjc 为强引用 {
        //
        //            }else{
        //
        //            }
    }
    /*
     *当block从堆中移除,就会调用此函数
     **/
    func mainBlockDispose0(srcBlock:MainBlockImpl0){
        blockObjectDispose(srcObjc: srcBlock.person!, type: 3)  //3表示 BLOCK_FIELD_IS_OBJECT
    }
    /*
     *会自动释放引用的auto变量,类似于 retainCount - 1 的操作
     **/
    func blockObjectDispose(srcObjc:AnyObject,type:Int){
        
    }
    
    class MJPerson: NSObject {
        var age: Int?
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        blockWithObjectLocalVar()
    }
    
    func blockWithObjectLocalVar(){ // 访问auto类型的对象类型(分为强引用(strong)和弱引用(weak))的局部变量的block
        
        let person = MJPerson()
        person.age = 10
        //        weak var weakPerson = person  //弱引用
        
        let mainBlockDesc0Data = MainBlockDesc0(reserved: 0, BlockSize: MemoryLayout.size(ofValue: MainBlockImpl0.self), copy: mainBlockCopy0,dipose: mainBlockDispose0)
        
        let block = MainBlockImpl0(fp: mainBlockFunc0, desc: mainBlockDesc0Data,flag: 0, _person: person)  //强引用
        
        //        let block = MainBlockImpl0(fp: mainBlockFunc0, desc: mainBlockDesc0Data, _person: weakPerson!) //弱引用
        
        person.age = 20
        
        block.imp?.FuncPtr!(block)
    }
}

你可能感兴趣的:(iOS原理之Block的访问auto变量(对象类型)的实现原理)