美文网首页
swift AssociatedObject关联属性

swift AssociatedObject关联属性

作者: _秃头少女_ | 来源:发表于2019-07-29 16:19 被阅读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

    相关文章

      网友评论

          本文标题:swift AssociatedObject关联属性

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