美文网首页
实现弱引用的NSArray NSMutableArray

实现弱引用的NSArray NSMutableArray

作者: lancely | 来源:发表于2016-04-29 11:27 被阅读662次

    添加一个分类

    NSMutableArray+WeakReferences.h

    #import <Foundation/Foundation.h>
    
    @interface NSMutableArray (WeakReferences)
    
    + (id)mutableArrayUsingWeakReferences;
    + (id)mutableArrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity;
    
    @end
    

    NSMutableArray+WeakReferences.m

    #import "NSMutableArray+WeakReferences.h"
    
    @implementation NSMutableArray (WeakReferences)
    
    + (id)mutableArrayUsingWeakReferences {
        return [self mutableArrayUsingWeakReferencesWithCapacity:0];
    }
    
    + (id)mutableArrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity {
        CFArrayCallBacks callbacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
        // Cast of C pointer type 'CFMutableArrayRef' (aka 'struct __CFArray *') to Objective-C pointer type 'id' requires a bridged cast
        return (id)CFBridgingRelease(CFArrayCreateMutable(0, capacity, &callbacks));
        // return (id)(CFArrayCreateMutable(0, capacity, &callbacks));
    }
    
    @end
    

    参考链接
    NSArray of weak references (__unsafe_unretained) to objects under ARC
    Non-retaining array for delegates

    相关文章

      网友评论

          本文标题:实现弱引用的NSArray NSMutableArray

          本文链接:https://www.haomeiwen.com/subject/krnhrttx.html