• objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAsso使用方法和场景


    原创文章,转载请注明出处:http://blog.sina.com.cn/s/blog_60342e330101tcz1.html

    1.    概念

    objective-c有两个扩展机制:category和associative。我们可以通过category来扩展方法,但是它有个很大的局限性,不能扩展属性。于是,就有了专门用来扩展属性的机制:associative。
     
    2.    使用方法
    在iOS开发过程中,category比较常见,而associative就用的比较少。associative的主要原理,就是把两个对象相互关联起来,使得其中的一个对象作为另外一个对象的一部分。
    使用associative,我们可以不用修改类的定义而为其对象增加存储空间。这在我们无法访问到类的源码的时候或者是考虑到二进制兼容性的时候是非常有用。
    associative是基于关键字的。因此,我们可以为任何对象增加任意多的associative,每个都使用不同的关键字即可。associative是可以保证被关联的对象在关联对象的整个生命周期都是可用的。
     
    associative机制提供了三个方法:

    OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, idvalue, objc_AssociationPolicy policy)
     
    OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)
     
    OBJC_EXPORT void objc_removeAssociatedObjects(id object)
     

     
    2.1.创建associative
    创建associative使用的是:objc_setAssociatedObject。它把一个对象与另外一个对象进行关联。该函数需要四个参数:源对象,关键字,关联的对象、关联策略。
    关键字是一个void类型的指针。每一个关联的关键字必须是唯一的。通常都是会采用静态变量来作为关键字。
    关联策略表明了相关的对象是通过赋值,保留引用还是复制的方式进行关联的;还有这种关联是原子的还是非原子的。这里的关联策略和声明属性时的很类似。这种关联策略是通过使用预先定义好的常量来表示的。
     
    比如,我们想对一个UIView,添加一个NSString类型的tag。可以这么做:

    - (void)setTagString:(NSString *)value {
             objc_setAssociatedObject(self, KEY_TAGSTRING, value,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

    2.2.获取associative对象
    获取相关联的是函数objc_getAssociatedObject。
    继续上面的例子,从一个UIView的实例中,获取一个NSString类型的tag

    - (NSString *)tagString {
             NSObject *obj = objc_getAssociatedObject(self, KEY_TAGSTRING);
             if (obj && [obj isKindOfClass:[NSString class]]) {
                  return (NSString *)obj;
        }
             return nil;
    }

     
    2.3.断开associative
    断开associative是使用objc_setAssociatedObject函数,传入nil值即可。

    objc_setAssociatedObject(self, KEY_TAGSTRING, nil,OBJC_ASSOCIATION_RETAIN_NONATOMIC);

     
    使用函数objc_removeAssociatedObjects可以断开所有associative。通常情况下不建议这么做,因为他会断开所有关联。
     
    3.   应用场景
    3.1.TagString
    上面的例子提到,在UIView中添加NSString类型的标记,就是一个非常实用的例子。全部的代码如下:
     

    @interface UIView(BDTag)
     
    @property (nonatomic, retain) NSString *tagString;
     
    - (UIView *)viewWithTagString:(NSString *)value;
     
    @end

     

    #import "UIView+BDTag.h"
     
    #undef   KEY_TAGSTRING
    #define KEY_TAGSTRING     "UIView.tagString"
     
    @implementation UIView(BDTag)
     
    @dynamic tagString;
     
    - (NSString *)tagString {
             NSObject *obj = objc_getAssociatedObject(self, KEY_TAGSTRING);
             if (obj && [obj isKindOfClass:[NSString class]]) {
                  return (NSString *)obj;
        }
            
             return nil;
    }
     
    - (void)setTagString:(NSString *)value {
             objc_setAssociatedObject(self, KEY_TAGSTRING, value,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
     
    - (UIView *)viewWithTagString:(NSString *)value {
             if (nil == value) {
                  return nil;
        }
            
             for (UIView *subview in self.subviews) {
                  NSString *tag = subview.tagString;
                  if ([tag isEqualToString:value])
                  {
                         return subview;
                  }
             }
            
             return nil;
    }
     
    @end

    苹果虽然有提供NSInteger类型的tag属性,用于标记相应的ui。但是在处理比较复杂的逻辑的时候,往往NSInteger类型的标记不能满足需求。为其添加了NSString类型的标记后。就能使用字符串,快速的标记ui,并且使用viewWithTagString方法,快速找到你所需要的ui。
     
    3.2.为NSObject子类添加任何信息
    这是一个方便,强大,并且简单的类。利用associative机制,为任何Object,添加你所需要的信息。比如用户登录,向服务端发送用户名/密码时,可以将这些信息绑定在请求的项之中。等请求完成后,再取出你所需要的信息,进行逻辑处理。而不需要另外设置成员,保存这些数据。
    具体的实现如下:

    @interface NSObject (BDAssociation)
     
    - (id)associatedObjectForKey:(NSString*)key;
    - (void)setAssociatedObject:(id)object forKey:(NSString*)key;
     
    @end

     

    #import
     
    #import "NSObject+BDAssociation.h"
     
    @implementation NSObject (BDAssociation)
     
    static char associatedObjectsKey;
     
    - (id)associatedObjectForKey:(NSString*)key {
        NSMutableDictionary *dict = objc_getAssociatedObject(self, &associatedObjectsKey);
        return [dict objectForKey:key];
    }
     
    - (void)setAssociatedObject:(id)object forKey:(NSString*)key {
        NSMutableDictionary *dict = objc_getAssociatedObject(self, &associatedObjectsKey);
        if (!dict) {
            dict = [[NSMutableDictionary alloc] init];
            objc_setAssociatedObject(self, &associatedObjectsKey, dict,OBJC_ASSOCIATION_RETAIN);
        }
        [dict setObject:object forKey:key];
    }
     
    @end
     

    3.3.内存回收检测
    记得在我刚开始学iOS开发的时候,经常出现内存泄露的问题,于是就在各个view controller的dealloc中打Log。这种方法虽然有效,但比较挫,不好管理。
    这里贴出一种漂亮的解决方案,利用associative机制。让object在回收时,自动输出回收信息。

    @interface NSObject (BDLogDealloc)
     
    - (void)logOnDealloc;
     
    @end



    #import "NSObject+BDLogDealloc.h"

    static char __logDeallocAssociatedKey__;
     
    @interface LogDealloc : NSObject
     
    @property (nonatomic, copy) NSString* message;
     
    @end
     
    @implementation NSObject (LogDealloc)
     
    - (void)logOnDealloc {
        if(objc_getAssociatedObject(self, &__logDeallocAssociatedKey__) == nil) {
            LogDealloc* log = [[[LogDealloc alloc] init] autorelease];
            log.message = NSStringFromClass(self.class);
            objc_setAssociatedObject(self, &__logDeallocAssociatedKey__, log,OBJC_ASSOCIATION_RETAIN);
        }
    }
     
    @end
     
    @implementation LogDealloc
     
    - (void)dealloc {
        NSLog(@"dealloc: %@", self.message);
        [_message release];
        [super dealloc];
    }
     
    @end
  • 相关阅读:
    gnome-shell 使用 notify-send 发送桌面消息
    fedora 中从命令行中直接打开资源管理器
    wget curl 的 使用方法
    NFS 系统的搭建 on fedora; nfs 搭建 on ubuntu 1806;
    -bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory
    Linux 快速查看系统配置-熟悉新环境的配置
    2019-11-7-WPF-How-to-get-plain-text-from-RichTextBox
    2018-8-10-git-push-错误-hook-declined-
    2018-12-25-win2d-图片水印
    2018-8-10-win10-UWP-用Path画图
  • 原文地址:https://www.cnblogs.com/mkai/p/6594668.html
Copyright © 2020-2023  润新知