关联对象 Associated Object

来源:互联网 发布:帽子淘宝网 编辑:程序博客网 时间:2024/04/27 20:37

需要在对象中存放某些信息,但是又无法从对象的所在类中继承一个子类,这时用到关联对象Associated Object。

例子 http://blog.csdn.net/lw223/article/details/21160767


管理关联对象

1 设置关联对象:  void objc_setAssociatedObject (id object, void *key, id value, objc_AssociationPolicy policy)

2 获取关联对象中的value: id objc_getAssociatedObject (id object, void *key)

3 移除指定对象中所有关联对象: void objc_removeAssociatedOjbects (id object) 


关联对象类型 objc_AssociationPolicy

/** * Policies related to associative references. * These are options to objc_setAssociatedObject() */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. */};



0 0
原创粉丝点击