iOS-实现弱引用的NSArray NSMutableArray

添加一个分类

NSMutableArray+WeakReferences.h

#import 

@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

转载于:https://www.cnblogs.com/lancely/p/5782760.html

你可能感兴趣的:(iOS-实现弱引用的NSArray NSMutableArray)