ios runtime objc_getAssociatedObject&objc_setAssociatedObject用法
来源:互联网 发布:毛笔字生成器软件下载 编辑:程序博客网 时间:2024/05/21 17:46
/** * 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);
我们打开runtime.h文件 看到api里面写到关于objc_getAssociatedObject与objc_setAssociatedObject的解释。
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);setAssociatedobject需要4个参数
//一个id object 源对象
// void *key key 唯一静态变量key associatedkey
// id value 关联的对象
//objc_AssociationPolicy policy 关键策略 一般是几个枚举构成
typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) { 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. */};
OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
//<span style="font-family: Arial;">id objc_getAssociatedObject(id object, const void *key)</span><pre name="code" class="objc" style="font-size: 14px;">同上一个源对象一个唯一静态变量key asssociatedkey
<pre name="code" class="objc">//// UIAlertView+alerview.h// MCLeftSilder//// Created by chuangqu on 16/7/22.// Copyright © 2016年 iOS. All rights reserved.//#import <UIKit/UIKit.h>typedef void (^successBlock)(NSInteger buttonIndex);@interface UIAlertView (alerview)<UIAlertViewDelegate>- (void)showWithBlock:(successBlock)block;@end
//// UIAlertView+alerview.m// MCLeftSilder//// Created by chuangqu on 16/7/22.// Copyright © 2016年 iOS. All rights reserved.//#import "UIAlertView+alerview.h"#import "objc/runtime.h"static const char alertKey;@implementation UIAlertView (alerview)- (void)showWithBlock:(successBlock)block{ if (block) { objc_setAssociatedObject(self, &alertKey, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } [self show];}- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ successBlock block = objc_getAssociatedObject(self, &alertKey); block(buttonIndex);}@end
这两个方法可以让一个对象和另一个对象关联,就是说一个对象可以保持对另一个对象的引用,并获取那个对象。有了这些,就能实现属性功能了。
UIAlertView关联了block 可以拿到block的对象,可以回调使用
1 0
- ios runtime objc_getAssociatedObject&objc_setAssociatedObject用法
- ios runtime 动态属性objc_setAssociatedObject和objc_getAssociatedObject
- ios runtime 动态属性objc_setAssociatedObject和objc_getAssociatedObject
- iOS关联,objc_getAssociatedObject, objc_setAssociatedObject
- iOS 关于objc_setAssociatedObject,objc_getAssociatedObject
- ios扩展机制objc_setAssociatedObject,objc_getAssociatedObject
- ios扩展机制objc_setAssociatedObject,objc_getAssociatedObject
- ios扩展机制objc_setAssociatedObject,objc_getAssociatedObject
- iOS-关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- objc_getAssociatedObject, objc_setAssociatedObject
- objc_getAssociatedObject, objc_setAssociatedObject
- objc_setAssociatedObject、objc_getAssociatedObject
- objc_getAssociatedObject, objc_setAssociatedObject
- objc_setAssociatedObject/objc_getAssociatedObject
- [IOS] 关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- IOS学习 关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObject)
- [iOS] objc_setAssociatedObject objc_getAssociatedObject 简单明了的一个理解
- [iOS] objc_setAssociatedObject objc_getAssociatedObject 简单明了的一个理解
- [李景山php]thinkphp核心源码注释|Memcached.class.php
- JVM内存结构
- Docker学习总结(10)——10分钟玩转Docker
- MINIX - 磁盘块和缓冲块
- source insight 添加ns2源码
- ios runtime objc_getAssociatedObject&objc_setAssociatedObject用法
- jquery获取触发事件相关的id
- HDU 1075 What Are You Talking About
- [李景山php]thinkphp核心源码注释|Memcachesae.class.php
- ThinkPHP函数详解:C方法
- 多播委托
- [李景山php]thinkphp核心源码注释|Redis.class.php
- Adapter大家族详解
- JavaScript精通到深入