Sha256: 219d5e1476ed5dd390d7fef82447a6e63b292100227fdd928374e4f07eb8016a

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

//
//  MAWeakDictionary.m
//  ZeroingWeakRef
//
//  Created by Mike Ash on 7/13/10.
//

#import "MAWeakDictionary.h"

#import "MAZeroingWeakRef.h"


@implementation MAWeakDictionary

- (id)init
{
    if((self = [super init]))
    {
        _dict = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (void)dealloc
{
    [_dict release];
    [super dealloc];
}

- (NSUInteger)count
{
    return [_dict count];
}

- (id)objectForKey: (id)aKey
{
    MAZeroingWeakRef *ref = [_dict objectForKey: aKey];
    id obj = [ref target];
    
    // clean out keys whose objects have gone away
    if(ref && !obj)
        [_dict removeObjectForKey: aKey];
    
    return obj;
}

- (NSEnumerator *)keyEnumerator
{
    // enumerate over a copy because -objectForKey: mutates
    // which could cause an exception in code that should
    // appear to be correct
    return [[_dict allKeys] objectEnumerator];
}

- (void)removeObjectForKey: (id)aKey
{
    [_dict removeObjectForKey: aKey];
}

- (void)setObject: (id)anObject forKey: (id)aKey
{
    [_dict setObject: [MAZeroingWeakRef refWithTarget: anObject]
                                               forKey: aKey];
}

@end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
motion-memorymanagement-0.1.0 MMRuntime/MMRuntime/MAWeakDictionary.m
motion-memorymanagement-0.0.1 MMRuntime/MMRuntime/MAWeakDictionary.m