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
原创粉丝点击