iOS中的集合该如何弱引用对象示例详解

2020-01-21 00:44:25刘景俊

前言

本文主要给大家介绍了关于iOS集合弱引用对象的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

1. 使用 NSValue

NSValue 可以弱引用保存一个对象,我们可以使用这种方法间接的引用。


NSValue *value = [NSValue valueWithNonretainedObject:@selector(class)];
[array addObject:value];

2. 使用 NSPointerArray,NSMapTable,NSHashTable

以上三个类型的用法分别对应 NSMutableArray,NSMutableDictionary,NSMutableSet。

NSPointerArray

NSPointerArray 和 NSMutableArray 一样同是有序可变集合,可插入、删除成员; 不同的是可以存储 NULL,且 count 可变,用 NULL 来填充。


// 实例化方法
- (instancetype)initWithOptions:(NSPointerFunctionsOptions)options;
- (instancetype)initWithPointerFunctions:(NSPointerFunctions *)functions;

NSPointerFunctionsOptions 枚举定义着内存管理策略、方法特性和内存标识,以下是几个常用的枚举值:

内存管理策略:

NSPointerFunctionsStrongMemory:强引用成员 NSPointerFunctionsMallocMemory 与 NSPointerFunctionsMachVirtualMemory: 用于 Mach 的 虚拟内存管理 NSPointerFunctionsWeakMemory:弱引用成员

方法特性:

NSPointerFunctionsObjectPersonality:hash、isEqual、对象描述 NSPointerFunctionsOpaquePersonality:pointer 的 hash 、直接判等

内存标识:

    NSPointerFunctionsCopyIn 添加成员时进行 copy 操作

选用多种组合方式:


NSPointerFunctionsOptions options = NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPersonality | NSPointerFunctionsCopyIn;
NSHashTable *table = [NSHashTable hashTableWithOptions:options];

static BOOL IsEqual(const void *item1, const void *item2, NSUInteger (*size)(const void *item)) {
  return *(const int *)item1 == *(const int *)item2;
}
NSPointerFunctions *functions = [[NSPointerFunctions alloc] init];
[functions setIsEqualFunction:IsEqual];

NSMapTable

NSMapTable 和 NSPointerArray 的初始化方法和使用都类似,不同的是 NSMapTable 的 key 和 object 各有不同的策略,所以有四种组合:

 

key object
weak weak
strong weak
weak strong
strong strong

 

如果 key 或者 object 是 weak 修饰时,任意一方在内存中被释放都会移除该键值对。

NSHashTable

NSHashTable 使用方法类似于 NSMutableSet,只不过该类型的 allObjectes 属性返回的是一个 NSArray,会对成员强引用。