zoukankan      html  css  js  c++  java
  • iOS 给已有的类添加属性.

    比如说给UIView 添加一个name 的属性,

    步骤:

    1. 创建 UIView + addProperty.h / UIView + addProperty.m 的类目,

    然后在UIView + addProperty.h 中添加一个属性,

    @property(nonatomic,strong)NSString *name;

     2. 在.m  中重写get /set 方法.

    @dynamic name;

    static char charKey;

    - (void)setName:(NSString *)name

    {

        objc_setAssociatedObject(self, &charKey, name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    }

    - (NSString *)name

    {

        return objc_getAssociatedObject(self, &charKey);

    }

    3. 解释一下 objc_setAssociatedObject 方法

     //1 源对象(在这里 是给view 添加的属性,所以 就是 view 自己)

        //2 关键字 唯一静态变量key associatedkey

        //3 关联的对象 ,这个地方是name值,sender

        //4 关键策略  OBJC_ASSOCIATION_ASSIGN

        //    enum {

        //        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. */

        //    };

  • 相关阅读:
    vi编辑器
    在shell脚本中使用函数
    在shell脚本中进行条件控制以及使用循环
    shell指令expr和test指令
    利用ps指令查看某个程序的进程状态
    shell变量的使用
    创建和运行shell脚本程序
    关于强制类型转换(c语言)
    elastic 常用查询操作
    elastic 集群安装
  • 原文地址:https://www.cnblogs.com/yinyakun/p/5882160.html
Copyright © 2011-2022 走看看