zoukankan      html  css  js  c++  java
  • 让两个对象间建立weak关系

    让两个对象间建立weak关系

    这是为了给两个对象间建立weak关系,当一个对象被释放时,另外一个对象再获取这个值时就是nil,也就是不持有这个对象:)

    源码:

    WeakRelatedDictionary.h 与 WeakRelatedDictionary.m

    //
    //  WeakRelatedDictionary.h
    //  TestDemo
    //
    //  Created by YouXianMing on 14-9-25.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface WeakRelatedDictionary : NSObject
    
    + (void)addObject:(id)object forKey:(id)key;
    + (id)objectForKey:(id)key;
    
    @end
    //
    //  WeakRelatedDictionary.m
    //  TestDemo
    //
    //  Created by YouXianMing on 14-9-25.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "WeakRelatedDictionary.h"
    
    static NSMapTable  *weakDictionary = nil;
    
    @implementation WeakRelatedDictionary
    
    + (void)initialize
    {
        if (self == [WeakRelatedDictionary class])
        {
            // 弱引用object弱引用object
            weakDictionary = [NSMapTable weakToWeakObjectsMapTable];
        }
    }
    
    + (void)addObject:(id)object forKey:(id)key
    {
        if (object == nil || key == nil)
        {
            NSLog(@"object or key should not be nil.");
            return;
        }
        
        if ([weakDictionary objectForKey:key] == nil)
        {
            [weakDictionary setObject:object forKey:key];
        }
    }
    
    + (id)objectForKey:(id)key
    {
        return [weakDictionary objectForKey:key];
    }
    
    @end

    NSObject+WeakRelated.h 与 NSObject+WeakRelated.m

    //
    //  NSObject+WeakRelated.h
    //  ScrollViewShowImage
    //
    //  Created by YouXianMing on 14-9-24.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface NSObject (WeakRelated)
    
    // 与对象弱相关联系
    - (void)setRelatedObject:(id)object;
    
    // 取出建立关系的对象
    - (id)relatedObject;
    
    @end
    //
    //  NSObject+WeakRelated.m
    //  ScrollViewShowImage
    //
    //  Created by YouXianMing on 14-9-24.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "NSObject+WeakRelated.h"
    #import "WeakRelatedDictionary.h"
    
    @implementation NSObject (WeakRelated)
    
    - (void)setRelatedObject:(id)object
    {
        [WeakRelatedDictionary addObject:object forKey:self];
    }
    
    - (id)relatedObject
    {
        return [WeakRelatedDictionary objectForKey:self];
    }
    
    @end

    测试代码:

    Model.h 与 Model.m

    //
    //  Model.h
    //  ObjectRelated
    //
    //  Created by YouXianMing on 14-9-27.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Model : NSObject
    
    @end
    //
    //  Model.m
    //  ObjectRelated
    //
    //  Created by YouXianMing on 14-9-27.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "Model.h"
    
    @implementation Model
    
    @end

    ViewController.m

    //
    //  ViewController.m
    //  ObjectRelated
    //
    //  Created by YouXianMing on 14-9-27.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "NSObject+WeakRelated.h"
    #import "WeakRelatedDictionary.h"
    #import "Model.h"
    
    @interface ViewController ()
    
    @property (nonatomic, strong) Model *model;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        _model             = [Model new];
        self.relatedObject = _model;
        
        NSLog(@"%@", self.relatedObject);
        
        [self performSelector:@selector(showInfo) withObject:nil afterDelay:3];
    }
    
    - (void)showInfo
    {
        _model = nil;
        NSLog(@"%@", self.relatedObject);
    }
    
    @end
  • 相关阅读:
    LCPhash求解
    BSGS
    洛谷—— P1849 [USACO12MAR]拖拉机Tractor
    BZOJ——2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
    洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing
    BZOJ——1601: [Usaco2008 Oct]灌水
    洛谷—— P1342 请柬
    [SDOI2009]Elaxia的路线 SPFA+Topo
    1737 配对
    51Nod 1378 夹克老爷的愤怒
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3997022.html
Copyright © 2011-2022 走看看