Collections Extention

NSPointerFunctiosn

The functions specified by an instance of NSPointerFunctions are separated into two clusters—those that define “personality” such as “object” or “C-string”, and those that describe memory management issues such as a memory deallocation function.

NSPointerArray、NSMapTable和NSHashTable使用NSPointerFunctions对象为存储其中的对象定义了获取和保留行为。内存和个性选项分为两大组:内存选项决定了内存管理,个性选项决定了哈希和相等。

  • NSPointerFunctionsStrongMemory(10.5~)

    使用strong写屏障后备存储,默认的内存选项。配合对象个性选项,使用retain和release;配合非对象选项,那就和NSPointerFunctionsMallocMemory一样。

  • NSPointerFunctionsZeroingWeakMemory(10.5~10.8)

    使用弱读写屏障,保留一个non-retained对象指针。

  • NSPointerFunctionsOpaqueMemory(10.5~)

    指针删除时没有任何操作,通常用来保留任意指针。获取内存方法只用于copy-in,释放内存方法没有任何动作。这个选项对于对象而言不是一个好选择。

  • NSPointerFunctionsMallocMemory(10.5~)

    用calloc()拷贝,用free()释放。

  • NSPointerFunctionsMachVirtualMemory(10.5~)

    用Mach内存

  • NSPointerFunctionsWeakMemory(10.8~)

    在ARC中使用weak读写屏障,释放后指针变为NULL。

  • NSPointerFunctionsObjectPersonality(10.5~)

    用hash实现哈希,用isEqual实现相等,用description实现描述,默认的个性选项。配合强内存选项,使用retain和release。

  • NSPointerFunctionsOpaquePersonality(10.5~)

    用移位指针实现哈希,用指针值实现相等。

  • NSPointerFunctionsObjectPointerPersonality(10.5~)

    用移位指针实现哈希,用指针值实现相等,用description实现描述。配合强内存选项,使用retain和release。

  • NSPointerFunctionsCStringPersonality(10.5~)

    用字符串的哈希方法实现哈希,用strcmp实现相等,用’%s’实现描述。

  • NSPointerFunctionsStructPersonality(10.5~)

    用内存哈希方法实现哈希,用memcmp实现相等。

  • NSPointerFunctionsIntegerPersonality(10.5~)

    用移位指针实现哈希,用指针值实现相等。

  • NSPointerFunctionsCopyIn(10.5~)

    把实体加入到集合中时,使用获取内存方法开辟内存并拷贝实体。


NSPointerArray

NSPointerArray is a mutable collection modeled after NSArray but it can also hold NULL values, which can be inserted or extracted (and which contribute to the object’s count). Moreover, unlike traditional arrays, you can set the count of the array directly.

方便的构造函数:

  • strongObjectsPointerArray(强对象数组)
  • weakObjectsPointerArray(若对象数组)

所有涉及到index的API,都需要注意其值不应该大于count,否则会有异常。allObjects方法可以将存储的所有有效对象(NULL被过滤)构建成一个NSArray,其中非对象实体会产生异常因为retain。

在性能方面,NSPointerArray真的非常非常慢,所以打算在一个很大的数据集合上使用它的时候一定要三思。


NSMapTable

NSMapTable is a mutable collection modeled after NSDictionary but provides different options:

  • The major option is to have keys and/or values held “weakly” in a manner that entries are removed when one of the objects is reclaimed.
  • Its keys or values may be copied on input or may use pointer identity for equality and hashing.
  • It can contain arbitrary pointers (its contents are not constrained to being objects).

方便的构造函数:

  • strongToStrongObjectsMapTable(强key强value)
  • weakToStrongObjectsMapTable(弱key强value)
  • strongToWeakObjectsMapTable(强key弱value)
  • weakToWeakObjectsMapTable(弱key弱value)

除了NSPointerFunctionsCopyIn,默认行为都会strong-retain(或weak-assign)key而不会copy,与CFDictionary行为相同而与NSDictionary不同。当需要一个字典,它的键没实现NSCopying协议时候非常有用。

NSMapTable只比NSDictionary略微慢一点。如果需要一个不retain键的字典,放弃CFDictionary使用它吧。


NSHashTable

NSHashTable is modeled after NSSet but provides different options, in particular to support weak relationships:

  • It can hold weak references to its members.
  • Its members may be copied on input or may use pointer identity for equality and hashing.
  • It can contain arbitrary pointers (its members are not constrained to being objects).

方便的构造函数:

  • weakObjectsHashTable(弱对象哈希表)
  • hashTableWithOptions:(某类型哈希表)

哈希表可以保持对对象的弱引用并在对象被销毁之后正确的将其移除 — 一些手动添加到NSSet的时候非常恶心的事情。

如果只是需要NSSet的特性,请坚持使用NSSet。NSHashTable在添加对象时花费了将近2倍的时间,但是其他方面的效率却非常相近。


扩展阅读

基础集合类


Collections Extention
https://hllovesgithub.github.io/2016/03/06/2016-02-28-Collections-Extention/
作者
Hu Liang
发布于
2016年3月6日
许可协议