zoukankan      html  css  js  c++  java
  • ios NSHashTable & NSMapTable

    在ios开发中大家用到更多的集合类可能是像NSSet或者NSDictionary,NSArray这样的。这里要介绍的是更少人使用的两个类,一个是NSMapTable,另一个是NSHashTable。

    NSHashTable

    NSHashTable看上去就像NSSet的替代品,对比NSSet/NSMutableSet,NSHashTable有以下的特性:

    • NSSet/NSMutableSet 拥有所包含对象的strong refrence。
    • NSHashTable是mutable可变的,没有不可变的类。
    • NSHashTable可以拥有成员的weak refrence;
    • NSHashTable可以在输入的时候copy成员。
    • NSHashTable可以包含任意的指针,可以使用指针去比较或者hash校验。

    NSHashTable在初始化的时候可以传入一个参数option,有以下几种选项

    • NSHashTableStrongMemory: Equal to NSPointerFunctionsStrongMemory. This is the default behavior, equivalent to NSSet member storage.
    • NSHashTableWeakMemory: Equal to NSPointerFunctionsWeakMemory. Uses weak read and write barriers. Using NSPointerFunctionsWeakMemory, object references will turn to NULL on last release.
    • NSHashTableZeroingWeakMemory: This option has been deprecated. Instead use the NSHashTableWeakMemory option.
    • NSHashTableCopyIn: Use the memory acquire function to allocate and copy items on input (see NSPointerFunction -acquireFunction). Equal to NSPointerFunctionsCopyIn.
    • NSHashTableObjectPointerPersonality: Use shifted pointer for the hash value and direct comparison to determine equality; use the description method for a description. Equal to NSPointerFunctionsObjectPointerPersonality.

    NSMapTable

    NSMapTable和NSDictionary有点类似,和dictionary相比,它有以下的特性:

    • NSDictionary和NSMutableDictionary会copy keys(这也是导致他们构造的时候性能相对低一点的原因),还会持有object的strong引用。
    • NSMapTable也都是mutable的,没有不可变类型。
    • NSMapTable也可以在input的时候copy它的值。
    • NSMapTable能包含任意指针,用指针来进行比较或者hash校验。

    类似的NSMapTable也有一个option参数来初始化,有以下几种选项

    • NSMapTableStrongMemory: Specifies a strong reference from the map table to its contents.
    • NSMapTableWeakMemory: Uses weak read and write barriers appropriate for ARC or GC. Using NSPointerFunctionsWeakMemory, object references will turn to NULL on last release. Equal to NSMapTableZeroingWeakMemory.
    • NSHashTableZeroingWeakMemory: This option has been superseded by the NSMapTableWeakMemory option.
    • NSMapTableCopyIn: Use the memory acquire function to allocate and copy items on input (see acquireFunction (see NSPointerFunction -acquireFunction). Equal to NSPointerFunctionsCopyIn.
    • NSMapTableObjectPointerPersonality: Use shifted pointer hash and direct equality, object description. Equal to NSPointerFunctionsObjectPointerPersonality.

    这里有一些关于集合类的参考链接:

  • 相关阅读:
    第一个spring MVC
    AOP切点相关
    设计模式详解
    Spring基本原理模拟(IoC部分)
    AOP常用注解
    Integer.valueOf与Integer.parseInt的小疑惑
    IE10与IMG图片PNG显示不了 WP中的WebBrowser中无法查看PNG格式的图片
    去除Coding4Fun中MessagePrompt的边框(Border)
    windows phone 中的TextBlock的一些特性(TextWrapping,TextWrapping)
    九度 找出两个只出现了一次的数字
  • 原文地址:https://www.cnblogs.com/tinkl/p/3667552.html
Copyright © 2011-2022 走看看