文档章节

Your New Friends: Obj-C Associated Objects

daveztong
 daveztong
发布于 2014/11/06 21:25
字数 293
阅读 13
收藏 0
There are tons of goodies in Snow Leopard for developers, but one of my favorites is associated objects, which allow you to tack arbitrary key/value pairs onto any  NSObject. Since there’s no documentation whatsoever, I thought I’d introduce you. Here’s how you’d store a value with a strong reference:
#import <objc/runtime.h>
objc_setAssociatedObject(obj, key, value, OBJC_ASSOCIATION_RETAIN);
And how you’d get it back:
id value = objc_getAssociatedObject(obj, key);
key can be any  void *; it doesn’t have to implement  NSCopying. See objc/runtime.h for the other memory management flags. If you’re defining a category on a class you didn’t make, associated objects give you a non-hideous way to essentially add instance variables. I use them in  KVO+Blocks to store observer data on objects. Associated objects also come in handy when you need to keep a value around for a little bit, but it’s just not important enough to muddy up your header. Maybe you’re firing off some dialog asynchronously, and you want to free it when the callback is fired. Associated objects save you from having to make an instance variable for that. Here’s a nice Obj-C category wrapping the C API. Enjoy! Update:  Kevin Ballard points out that since the key is just a  void *, you can also do this kind of thing:
static char key;
objc_setAssociatedObject(obj, &key, ...);
This is especially elegant when  key can be method-local—when lazily loading some resource, for instance. Update 2:  Bill Bumgarner further  points out that  _cmd can also be used as the key, since selectors are constant and unique. From:http://blog.andymatuschak.org/post/173646741/your-new-friends-obj-c-associated-objects 结合block来改写UIAlertView和UIActionSheet很有用!

© 著作权归作者所有

下一篇: Nginx+PHP配置
daveztong
粉丝 4
博文 93
码字总数 21482
作品 0
成都
程序员
私信 提问
Java 基本数据类型 sizeof 功能

Java基本数据类型 int 32bit short 16bit long 64bit byte 8bit char 16bit float 32bit double 64bit boolean 1bit,This data type represents one bit of information, but its "size" is......

长平狐
2013/01/06
92
0
Associated Objects

Written by Mattt Thompson on Feb 10th, 2014 #import <objc/runtime.h> Objective-C developers are conditioned to be wary of whatever follows this ominous incantation. And for good......

whj
2014/03/09
43
0
聊聊flink的BlobService

序 本文主要研究一下flink的BlobService BlobService flink-release-1.7.2/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobService.java BlobService定义了getPermanentBlo......

go4it
02/27
13
0
Objective-C中的runtime详解_1

Objective-C中的runtime详解 *** 本文介绍自己用到过的或者遇到过的runtime函数, 持续更新中... 首先要想使用runtime中的函数, 必须引入头文件. 1.objc打头的方法 1.1 objc_getClass() 这个方...

hell03W
2016/03/29
24
0
《.Ne框架程序设计》随记(5)

实现Equals方式有3种不同的方式: 1)为基类没有重写Object.Equals方法的引用类型实现Equals class MyRefType : BaseType { RefType refobj; // This field is a reference type. ValType va......

嗯哼9925
2017/12/25
0
0

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周六乱弹 —— 不要在领导修风扇的时候打开电扇

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @巴拉迪维 :《Whats Up》 主唱妹子 Lina Perry 的嗓音实在太有力了,收放自如的自信才能唱出这么优秀的歌吧!#今日歌曲推荐# 《Whats Up》-...

小小编辑
今天
11
0
SpringBoot集成Elasticsearch并进行增删改查操作

一、创建工程 使用IntelliJ创建SpringBoot工程 SpringBoot版本为2.0.4 ElasticSearch为5.6.10 删掉蓝框中的文件(如上) 最后我们的目录结构(如下) 下面pom文件主要修改的是把spring boot从Int...

一字见心
今天
5
0
x001-版本介绍

python版本介绍 目前有2 和 3 有很多企业用的代码是2版本,随着技术的发展,以后用3的会成为大趋势 python3的安装 yum -y install wget gcc zlib-devel bzip2-devel openssl-devel ncurses-d...

侠客行之石头
今天
5
0
聊聊rocketmq的TransientStorePool

序 本文主要研究一下rocketmq的TransientStorePool TransientStorePool rocketmq-all-4.6.0-source-release/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java publi......

go4it
昨天
6
0
笔记

场外借贷, 质押 ,托管, 永续合约. 场外借贷,n签合同. 新功能 证券交易组负责中信证券机构及个人投资交易相关系统,服务机构及个人投资客户, 涉及到两融、期权、 期货、做市等境内境外创新业...

MtrS
昨天
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部