Go [IOS] Nshash? Table & Nsmap? Table

Source: Internet
Author: User

NSSetNSDictionaryand, along with is the NSArray workhorse collection classes of Foundation. Unlike other standard libraries, implementation details is hidden from developers, allowing them to write simple code and Trust that it'll be (reasonably) performant.

However, even the best abstractions; Their underlying assumptions overturned. In these cases, developers either venture further off the abstraction, or, if available use a more general-purpose Soluti Mnl

NSSet NSDictionary for and, the breaking assumption is in the memory behavior when storing objects in the collection. NSSetfor, objects is a strongly referenced, as is NSDictionary values. Keys, on the other hand, is copied by NSDictionary . If a developer wanted to store a weak value, or use a non- <NSCopying> -conforming object as a key, they could is clever and use c6/>. Or, as of IOS 6 (and as far back as OS X Leopard), they could with NSHashTable or NSMapTable , the more general-case counterparts to or NSDictionary , respectively.

So without further ado, here's everything you need to know about both of the more obscure members of Foundation ' s Collectio N classes:

NSHashTable

NSHashTableis a general-purpose analogue of NSSet . Contrasted NSSet NSMutableSet with the behavior of/, have the NSHashTable following characteristics:

    • NSSet/ NSMutableSet holds strong references to members, which is tested for hashing and equality using the methods hash and isEqual: .
    • NSHashTableis mutable, without an immutable counterpart.
    • NSHashTableCan hold the references to their members weak .
    • NSHashTableCan members on copy input.
    • NSHashTablecan contain arbitrary pointers, and use pointer identity for equality and hashing checks.
Usage
Nshashtable*HashTable=[NshashtableHashtablewithoptions:nspointerfunctionscopyin]; [hashtable addobject:@ "foo" ];< Span class= "p" >[hashtable addobject:@ "bar" ; [hashtable addobject:@42; [hashtable removeobject:@ "bar" ; nslog (@ "Members:%@" [ hashtable allobjects        

NSHashTableObjects is initialized with a option for any of the following behaviors. Deprecated enum values is due to NSHashTable being ported from garbage-collected OS X to arc-ified IOS. Other values is aliased to the options defined by Nspointerfunctions, which would be covered next week on Nshipster.

  • NSHashTableStrongMemory: Equal to NSPointerFunctionsStrongMemory . This was the default behavior, equivalent to NSSet member storage.
  • NSHashTableWeakMemory: Equal to NSPointerFunctionsWeakMemory . Uses weak read and write barriers. Using NSPointerFunctionsWeakMemory , the object references would turn to on the last NULL release.
  • NSHashTableZeroingWeakMemory: This option has been deprecated. Instead Use the NSHashTableWeakMemory option.
  • NSHashTableCopyIn: Use the memory acquire function to allocate and copy items in 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

NSMapTableis a general-purpose analogue of NSDictionary . Contrasted NSDictionary NSMutableDictionary with the behavior of/, have the NSMapTable following characteristics:

    • NSDictionary/ NSMutableDictionary copies keys, and holds strong references to values.
    • NSMapTableis mutable, without an immutable counterpart.
    • NSMapTableCan hold keys weak and values with references, in such a-on-entries is removed when either the key or value is DEA Llocated.
    • NSMapTableCan's copy values on input.
    • NSMapTablecan contain arbitrary pointers, and use pointer identity for equality and hashing checks.
Usage

Instances where one might use NSMapTable include non-copyable keys and storing weak references to keyed delegates or another Ki nd of weak object.

IdDelegate= ...; nsmaptable *maptable = [nsmaptable maptablewithkeyoptions: Nsmaptablestrongmemory valueoptions:nsmaptableweakmemory ; [maptable setobject:delegate Span class= "NL" >forkey:@ "foo" ]; Nslog (@ "Keys:%@" [[maptable span class= "n" >keyenumerator allobjects     

nsmaptable  objects is initialized with options specifying behavior for both keys and values, using t He following Enum values:

    • 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 would turn to  NULL  on Last release. Equal to  nsmaptablezeroingweakmemory .
    • nshashtablezeroingweakmemory : This option have been superseded by the  Nsmaptableweakmemory  option.
    • nsmaptablecopyin : Use the memory acquire function to allocate and copy items in input (see Acquirefunctio N (see  nspointerfunction-acquirefunction ). Equal to Nspointerfunctionscopyin.
    • nsmaptableobjectpointerpersonality : Use shifted pointer hash and direct equality, object description. Equal to  nspointerfunctionsobjectpointerpersonality .
Subscripting

NSMapTableDoesn ' t implement object subscripting, but it can is trivially added in a category. NSDictionary' s NSCopying requirement for keys belongs to NSDictionary alone:

@implementationNsmaptable(nshipstersubscripting)-(Id)Objectforkeyedsubscript:(Id) key{return [ Self objectforkey:key];} - (voidsetobject:  (id) obj forkeyedsubscript:< Span class= "P" > (id) key{[self setobject:obj forkey :key];}  @end                

As always, it's important to remember that programming are not on being clever:always approach a problem from the Highe St viable level of abstraction. and is NSSet NSDictionary great classes. For 99% of problems, they is undoubtedly the correct tool for the job. If, however, your problem have any of the particular memory management constraints described above, then NSHashTable & NSMapTable M Ay is worth a look.

---------

The collection of weak references as delegate is more appropriate.

Transferred from: http://nshipster.com/nshashtable-and-nsmaptable/

Go [IOS] Nshash? Table & Nsmap? Table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.