让两个对象间建立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
原文地址:https://www.cnblogs.com/YouXianMing/p/3997022.html