Objective-C Associated Objects 初识

Associated Objects 主要有以下三个使用场景:

1、为现有的类添加私有变量以帮助实现细节

2、为现有的类添加公有属性;

3、为 KVO 创建一个关联的观察者。

关联函数一共有3个:

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

//用于给对象添加关联对象,传入 nil 则可以移除已有的关联对象

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

//用于获取关联对象

void objc_removeAssociatedObjects(id object);

//用于移除一个对象的所有关联对象

注:objc_removeAssociatedObjects 函数我们一般是用不上的,因为这个函数会移除一个对象的所有关联对象,将该对象恢复成“原始”状态。这样做就很有可能把别人添加的关联对象也一并移除,这并不是我们所希望的。所以一般的做法是通过给 objc_setAssociatedObject 函数传入 nil 来移除某个已有的关联对象

key值的声明:

*声明 static char kAssociatedObjectKey; 使用&kAssociatedObjectKey

*用 selector ,使用 getter 方法的名称作为 key 值

OBJC_ASSOCIATION_ASSIGN = 0,                //弱引用关联对象

OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,   //强引用关联对象,且为非原子操作

OBJC_ASSOCIATION_COPY_NONATOMIC = 3,    //复制关联对象,且为非原子操作

OBJC_ASSOCIATION_RETAIN = 01401,          //强引用关联对象,且为原子操作

OBJC_ASSOCIATION_COPY = 01403        //复制关联对象,且为原子操作

时间: 2024-08-06 19:50:24

Objective-C Associated Objects 初识的相关文章

[OpenGL]环境搭建以及OpenGL初识

想往游戏行业发展的话,经常被提及到的就是OpenGL和DirectX,这两者听起来感觉是一门挺高深的技术,今天我也开始摸索学习OpenGL,那么OpenGL到底是什么?它和DirectX有什么区别和联系? OpenGL初识 OpenGL只是一套图形函数库 DirectX包含图形.声音.输入.网络等模块. 但就图形而论,DirectX的图形库性能不如OpenGL,OpenGL稳定,可以跨平台使用,DirectX只支持Windows平台,所以OpenGL还是有它的优势!OpenGL ES是OpenG

C++ vs Objective C

oc Short list of some of the major differences: C++ allows multiple inheritance, Objective-C doesn't.一个允许多继承,一个不允许Unlike C++, Objective-C allows method parameters to be named and the method signature includes only the names and types of the parameter

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

Objective -C Memory Management??内存管理??第一部分 Memory management is part of a more general problem in programming called resource management. 内存管理是资源管理的一部分. Every computer system has finite resources for your program to use. These include memory, open fi

Objective -C Object initialization 对象初始化

Objective -C Object initialization 对象初始化 1.1 Allocating Objects  分配对象 Allocation is the process by which a new object is born. allocation 是新对象诞生的过程. Sending the alloc message to a class causes that class to allocate a chunk of memory large enough to

#从零开始学Swift2.0# No.1 初识Swift

前言 WWDC2014Apple Inc发布Swift1.0,   从2014年到2015年WWDC2015, Swift经历了1.0, 1.1, 1.2和现在刚发布的2.0版本. 从1.0版本到2.0版本,Swift的语法体系结构改动非常大. 去年的时候学习过一点点Swift的基本语法, 但是由于当时Swift这门语言太动荡, 所以一直页没有去深入学习,  渐渐的基本都已经还给了Apple Inc. 随着2015年12月4日的到来, Apple Inc如约在GitHub上开源了Swift. A

Method and Apparatus for Providing Highly-Scalable Network Storage for Well-Gridded Objects

An apparatus comprising a plurality of storage nodes comprising a plurality of corresponding storage disks and configured to store data in a distributed manner between the storage disks that achieves a Redundant Array of Independent Disks-0 (RAID0) l

Objective -C Categories

Objective -C Categories? The dynamic runtime dispatch mechanism employed by Objective-C lets you add methods to existing classes. 被Objective -C 采纳的动态运行分配机制让你可以在已有的类中添加方法. 1.1 Creating a Category 创建一个Category? A category is a way to add new methods to

Django补充及初识Ajax

Django创建一对多表结构 首先现在models.py中写如下代码: from django.db import models # Create your models here. class Business(models.Model): caption = models.CharField(max_length=32) class Host(models.Model): nid = models.AutoField(primary_key=True) hostname = models.C

Objective - c Foundation 框架详解2

Objective - c  Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such as NSArray and NSDictionary whose instances exist just to hold onto other objects. cocoa 提供了一系列的集合类,例如,NSarray,NSdictionary.它们存在的目的就是为了保持其他对象. 1.1.1N