一、相关函数

与Associated Objects相关的函数有三个

1 void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);

2 id objc_getAssociatedObject(id object, const void *key);

3 void objc_removeAssociatedObjects(id object);

其中常用的函数是前两个,第三个不经常用。

简单来说,objc_setAssociatedObject用于关联对象,objc_getAssociatedObject用来获取关联的对象,objc_removeAssociatedObjects用来移除关联的对象。

二、object、key、value、policy

object:你所关联的对象。

key:所用的key值(就像key有钥匙的含义,通过key,你就可以找到这个对象关联的值)

value:所关联的值。

policy:关联策略(其实就是那几个引用)。

 三、key

1.声明 static char overviewKey;,使用&overviewKey作为key值;

2.声明 static void *overviewKey = &overviewKey;,使用overviewKey作为key值;

3.用selevtor,使用getter方法的名称作为key值。

需要说明的是,key不需要赋值,只需声明。

四、关联策略

给对象添加关联对象时有五种关联策略可供选择:

关联策略 等价属性   说明
OBJC_ASSOCIATION_ASSIGN

@property (assign)

or@property (unsafe_unretained)

弱引用关联对象
OBJC_ASSOCIATION_RETAIN_NONATOMIC @property (strong, nonatomic) 强引用关联对象,且为非原子操作
OBJC_ASSOCIATION_COPY_NONATOMIC @property (copy, nonatomic) 复制关联对象,且为非原子操作
OBJC_ASSOCIATION_RETAIN @property (strong, atomic) 强引用关联对象,且为原子操作
OBJC_ASSOCIATION_COPY @property (copy, atomic) 复制关联对象,且为原子操作

五.简单的代码

static char stringKey;

NSArray *array = [[NSArray alloc]initWithObjects:@"One",@"Two",@"Three", nil];

NSString * string = @"这是一个测试";

objc_setAssociatedObject(array, &stringKey, string, OBJC_ASSOCIATION_RETAIN);//在这里将string与array关联

NSString *associatedObject = (NSString *)objc_getAssociatedObject(array, &stringKey);//这里将string的值赋给associatedObject

参考链接

http://blog.leichunfeng.com/blog/2015/06/26/objective-c-associated-objects-implementation-principle/

http://blog.csdn.net/quanqinyang/article/details/38017581

欢迎有错进行纠正,一起进步。

关于Objective-C Associated Objects的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. Object Pascal中文手册 经典教程

    Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...

  3. 理解Objective C 中id

    什么是id,与void *的区别 id在Objective C中是一个类型,一个complier所认可的Objective C类型,跟void *是不一样的,比如一个 id userName, 和vo ...

  4. C++ vs Objective C

    oc Short list of some of the major differences: C++ allows multiple inheritance, Objective-C doesn't ...

  5. represent states with objects

    1. The behavior of objects in the real world is more complex than simply being in one state at a tim ...

  6. #学习笔记# VALSE 2019.01.09 朱俊彦 --- Learning to Synthesize Images, Videos, and 3D Objects

    视频类型:VALSE-webinar 报告时间:2019年01月09日 报告人:MIT朱俊彦 报告题目:Learning to Synthesize Images, Videos, and 3D Ob ...

  7. Objective -C Categories

    Objective -C Categories  The dynamic runtime dispatch mechanism employed by Objective-C lets you add ...

  8. Objective -C Object initialization 对象初始化

    Objective -C Object initialization 对象初始化 1.1 Allocating Objects  分配对象 Allocation is the process by w ...

  9. Objective -C Memory Management 内存管理 第一部分

    Objective -C Memory Management  内存管理  第一部分 Memory management is part of a more general problem in pr ...

  10. Objective - c Foundation 框架详解2

    Objective - c  Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...

随机推荐

  1. Parameter Binding in ASP.NET Web API(参数绑定)

    Parameter Binding in ASP.NET Web API(参数绑定) 导航 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnbl ...

  2. [Usaco2007 Dec]穿越泥地[bfs][水]

    Description 清早6:00,Farmer John就离开了他的屋子,开始了他的例行工作:为贝茜挤奶.前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想见,FJ 现在面对的是一大片泥泞 ...

  3. 服务端套接字类CxServerSocket的使用

    服务端套接字类CxServerSocket的使用 这是一个精练的服务端套接字类,类名.函数名和变量名均采用匈牙利命名法.小写的x代表我的姓氏首字母(谢欣能),个人习惯而已,如有雷同,纯属巧合. CxS ...

  4. 自定义html标签

    自定义html标签 (觉得页面全是div 的解决办法 来个个性化) 办法一(来源于 让ie支持html5标签的方法): 在ie9以前如果要自定义要使用document.createElement的方法 ...

  5. Python闭包详解

    Python闭包详解 1 快速预览 以下是一段简单的闭包代码示例: def foo(): m=3 n=5 def bar(): a=4 return m+n+a return bar >> ...

  6. 搭建基于SSI(struts2,spring,ibatis)的javaEE开发环境

    搭建基于SSI(struts2,spring,ibatis)的javaEE开发环境 最近有很多人不知道如何搭建基于SSI(struts2,spring,ibatis)的J2EE开发环境,这里给大家一个 ...

  7. MS SQL SERVER 2008 使用OBJECT_ID判断临时表是否存在

    MS SQL SERVER 2008 使用OBJECT_ID判断临时表是否存在 我们在写sql 脚本的时候经常会用到临时表,有时间也需要根据临时表是否存在做一些逻辑处理.还好sql server已经集 ...

  8. MacOSX高分屏图片打包工具tiffutil的简单使用

    You can use the man command tiffutil with the option -cathidpicheck. The command lets you manipulate ...

  9. linux命令——iotop

    查看CPU使用情况用top,查看I/O使用情况就需要iotop.这个命令是在 kernel v2.6.20中添加,安装的时候要注意内核的版本号. iotop常用快捷键 1. 左右箭头 --> 改 ...

  10. LearnCpp.com

    LearnCpp.com is a totally free website devoted to teaching you to program in C++. Whether you’ve had ...