ios runtime objc_getAssociatedObject&objc_setAssociatedObject用法

本文介绍Objective-C中objc_setAssociatedObject与objc_getAssociatedObject两个API的使用方法,通过实例展示了如何利用这两个API实现对象间关联及引用,为实现属性等功能提供支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/** 
 * 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的对象,可以回调使用




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值