swift AssociatedObject关联属性

2019-07-29  本文已影响0人  _秃头少女_

import Foundation
import UIKit

private var kpageParams = "pageParams"
extension UIViewController{
    func getPageParams(){
        self.pageParams = "关联属性"
        print(self.pageParams)
    }
    
    var pageParams:NSString? {
        get{
            guard let size = objc_getAssociatedObject(self, &kpageParams) as? NSString else {
                return nil
            }
            return size
        }
        set(newValue){
            objc_setAssociatedObject(self, &kpageParams, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        
    }
    
    
}


typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
    OBJC_ASSOCIATION_ASSIGN = 0,           /**< Specifies a weak reference to the associated object. */
    OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object. The association is not made atomically. */
    OBJC_ASSOCIATION_COPY_NONATOMIC = 3,   /**< Specifies that the associated object is copied. The association is not made atomically. */
    OBJC_ASSOCIATION_RETAIN = 01401,       /**< Specifies a strong reference to the associated object. The association is made atomically. */
    OBJC_ASSOCIATION_COPY = 01403          /**< Specifies that the associated object is copied. The association is made atomically. */
};

https://www.jianshu.com/p/1feae48a5dda

上一篇下一篇

猜你喜欢

热点阅读