添加一个分类
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
网友评论