swift objc_setAssociatedObject block(version1.2 xcode6.4)

 

import UIKit

class LSObjectWrapper: NSObject {
    let value: ((barButton: UIButton?) -> Void)?
    
    init(value: (barButton: UIButton?) -> Void) {
        self.value = value
    }
}

 

    private var LSActionBlockKey = 0

    var actionBlock:((barButton: UIButton?) -> Void)?{
        get{
            let wrapper:LSObjectWrapper? = objc_getAssociatedObject(self, &LSActionBlockKey) as? LSObjectWrapper
            let block = wrapper?.value
            return block;
        }
        set{
            let wrapper = LSObjectWrapper(value: newValue!)
                objc_setAssociatedObject(self, &LSActionBlockKey, wrapper, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
        }
    }

 

weak等类型类似

 

 

 

 

 

 

 

你可能感兴趣的:(version)