-
Notifications
You must be signed in to change notification settings - Fork 52
Objective C 容器
Objective-C 中的 NSArray、NSSet、NSDictionary 与 NSObject 及其子类对象的 hash、isEqual 方法有许多联系
NSSet 是无序的元素序列,集合内元素不允许重复
- 主要通过比较访问元素
- 无序
- 元素不重复
NSSet
不允许添加重复元素,所以添加新元素时,该元素的 hash
方法会被调用。若集合中不存在与此元素 hash 值相同的元素,则它直接被加入集合,不调用 isEqual
方法;若存在,则调用集合内的对应元素的 isEqual
方法,返回真值则判相等,不加入,处理结束。若返回 false,则判定集合内不存在该元素,将其加入。
从集合中移除元素时,首先调用它的 hash
方法。若集合中存在与其 hash 值相等的元素,则调用该元素的 isEqual
方法,若真值则判等,进行移除;若不存在,则会依次调用集合中每个元素的 isEqual
方法,只要找到一个返回真值的元素,就进行移除,并结束整个过程。(所以这样会有其他满足 isEqual
方法但却被漏掉未被移除的元素)。调用 contains
方法时,过程类似。
[set addObject:@1];
[set addObject:@2];
[set addObject:@3];
[set addObject:@4];
[set addObject:@6];
[set addObject:@4];
[set addObject:@1];
[set addObject:@2];
//output: [1, 2, 6, 4, 3]
数组是一个有序的元素序列(添加时保持有序),具有以下特点
- 支持通过索引访问元素
- 有序
- 允许重复
NSArray 允许添加重复元素,添加元素时不查重,所以不调用上述两个方法。在移除元素时,会对当前数组内的元素进行遍历,每个元素的 isEqual 方法都会被调用(使用 remove 方法传入的元素作为参数),所有返回真值的元素都被移除。在字典中,不涉及 hash 方法。
[array addObject:@1];
[array addObject:@2];
[array addObject:@3];
[array addObject:@4];
[array addObject:@6];
[array addObject:@4];
[array addObject:@1];
[array addObject:@2];
//output: [1, 2, 3, 4, 6, 4, 1, 2]
When the order of the items in the collection is not important, sets offer better performance for finding items in the collection.The reason is that a set uses hash values to find items (like a dictionary) while an array has to iterate over its entire contents to find a particular object.
A collection similar to an array, but with a broader range of available memory semantics.
- The pointer array class is modeled after NSArray, but can also hold nil values. You can insert or remove nil values which contribute to the array's count.
- A pointer array can be initialized to maintain strong or weak references to objects, or according to any of the memory or personality options defined by NSPointerFunctions.Options.
- The NSCopying and NSCoding protocols are applicable only when a pointer array is initialized to maintain strong or weak references to objects.
- When enumerating a pointer array with NSFastEnumeration using for...in, the loop will yield any nil values present in the array.
A collection similar to a dictionary, but with a broader range of available memory semantics.
- Keys and/or values are optionally held “weakly” such 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).
You can configure an NSMapTable instance to operate on arbitrary pointers and not just objects, although typically you are encouraged to use the C function API for void * pointers. The object-based API (such as setObject(_:forKey:)) will not work for non-object pointers without type-casting.
When configuring map tables, note that only the options listed in NSMapTableOptions guarantee that the rest of the API will work correctly—including copying, archiving, and fast enumeration.
A collection similar to a set, but with broader range of available memory semantics.The hash table is modeled after NSSet with the following differences:
- 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).
You can configure an NSHashTable instance to operate on arbitrary pointers and not just objects, although typically you are encouraged to use the C function API for void * pointers. The object-based API (such as add(_:)) will not work for non-object pointers without type-casting.
Because of its options, NSHashTable is not a set because it can behave differently (for example, if pointer equality is specified two isEqual: strings will both be entered).
When configuring hash tables, note that only the options listed in NSHashTableOptions
guarantee that the rest of the API will work correctly—including copying, archiving, and fast enumeration. While other NSPointerFunctions
options are used for certain configurations, such as to hold arbitrary pointers