iOS开发--开发细节(使用关联对象存取自定义数据)
来源:互联网 发布:远程会诊软件 编辑:程序博客网 时间:2024/06/11 08:30
关联对象可以给一个定义好的对象 增加一个KEY从而来存取自定义数据
提供了三个API
/** **设置** * Sets an associated value for a given object using a given key and association policy. * * @param object The source object for the association. * @param key The key for the association. * @param value The value to associate with the key key for object. Pass nil to clear an existing association. * @param policy The policy for the association. For possible values, see “Associative Object Behaviors.” * * @see objc_setAssociatedObject * @see objc_removeAssociatedObjects */OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);/** **读取** * Returns the value associated with a given object for a given key. * * @param object The source object for the association. * @param key The key for the association. * * @return The value associated with the key \e key for \e object. * * @see objc_setAssociatedObject */OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);/** **删除** * Removes all associations for a given object. * * @param object An object that maintains associated objects. * * @note The main purpose of this function is to make it easy to return an object * to a "pristine state”. You should not use this function for general removal of * associations from objects, since it also removes associations that other clients * may have added to the object. Typically you should use \c objc_setAssociatedObject * with a nil value to clear an association. * * @see objc_setAssociatedObject * @see objc_getAssociatedObject */OBJC_EXPORT void objc_removeAssociatedObjects(id object) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
objc_AssociationPOlicyen Enum
typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) { OBJC_ASSOCIATION_ASSIGN = 0, //相当于属性中的assign OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, //retain,monatomic OBJC_ASSOCIATION_COPY_NONATOMIC = 3, //copy,nonatomic OBJC_ASSOCIATION_RETAIN = 01401, //retain OBJC_ASSOCIATION_COPY = 01403 //copy };
Demo 给button 增加了一个string
#import "ViewController.h"#import <objc/runtime.h>static void * myBtnKey = "myBtnKey";@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.backgroundColor = [UIColor blackColor]; btn.frame = CGRectMake(100, 100, 60, 30); [self.view addSubview:btn]; objc_setAssociatedObject(btn, myBtnKey, @"mybtn", OBJC_ASSOCIATION_RETAIN_NONATOMIC); [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)btnClick:(id)sender { NSString *str = objc_getAssociatedObject(sender, myBtnKey); /** * CODE */}
0 0
- iOS开发--开发细节(使用关联对象存取自定义数据)
- iOS开发 数据存取方法 对象归档
- iOS开发-----数据存取
- iOS开发--数据存取
- iOS开发之数据存取3-CoreData自定义数据类型
- iOS开发之数据存取3-CoreData自定义数据类型Transformable
- iOS开发系列--数据存取
- iOS开发系列--数据存取
- iOS开发之数据存取
- iOS开发系列--数据存取
- iOS开发系列--数据存取
- iOS开发系列--数据存取
- iOS开发系列--数据存取
- iOS开发系列--数据存取
- iOS开发系列--数据存取
- iOS开发系列--数据存取
- iOS开发系列--数据存取
- iOS开发系列--数据存取
- RecyclerView 获取item,删除item的坑
- 开博客的第一天
- 约束理论(TOC)的生产计划与排程管理
- Nginx rewrite(重读)
- sublime笔记
- iOS开发--开发细节(使用关联对象存取自定义数据)
- 1.4计算机的特点与性能指标
- (一)线程------JVM体系结构与内存模型概要
- SwipeRefreshLayout的用法
- Tachyon:Spark生态系统中的分布式内存文件系统
- The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone.
- 深入理解Java:注解(Annotation)--注解处理器
- Android recovery分析(二)---增量升级包的编译流程
- Android 使用handler循环发送消息