码迷,mamicode.com
首页 > 其他好文 > 详细

让两个对象间建立weak关系

时间:2014-09-27 19:04:10      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   strong   for   sp   

让两个对象间建立weak关系

bubuko.com,布布扣

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

bubuko.com,布布扣

源码:

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

 

让两个对象间建立weak关系

标签:style   blog   http   color   io   ar   strong   for   sp   

原文地址:http://www.cnblogs.com/YouXianMing/p/3997022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!