Nshashtable and Nsmaptable

Source: Internet
Author: User

Nsset, Nsdictionary, Nsarray is the foundation Framework's common class for collection operations, andother standard set of operations librariesDifferent, the way they implement it is for developersHidden,Only allow developers to write some simple code that allows them to believe that the code has a reason to work properly. However, the best code abstraction will be broken, and Apple's intentions are distorted. In this case, the developer seeks a better abstraction to use the collection, or to look for a more generic way. For Nsset and nsdictionary, breaking the Code abstraction style is the way they access object in memory. In Nsset, objects is strongly quoted (strongly referenced), and the keys and values in Nsdictionary are also copied nsdictionary. If a developer wants toStore a value of type weak or use an object that does not implement the Nscopying protocol as the Nsdictionary keyThat he might be very smart to thinkNsvalue +valuewithnonretainedobject。 IOS6 and OSX 10.5, can be used respectively and nsset, nsdictionary status of the same nshashtable,nsmaptable. These two classes are not commonly used in the collection of the foundation, and in order to avoid your confusion, the usage of these two classes is described below. nshashtableNshashtable is a more extensive nsset, unlike Nsset/nsmutableset,nshashtable, which has the following characteristics: * Nsset/nsmutableset is a strongly typed reference to its members, These members are hashed and isequal through the hash and contrast functions * nshashtable is mutable * nshashtable can hold member variables of weak type * Nshashtable can copy members when adding member variables * Nshashtable can store pointers arbitrarily and use the uniqueness of pointers for hash identity checks (check for duplicate member variables) and contrast operations (equal), using the following:
    nshashtable *hashtable = [nshashtable hashtablewithoptions:nspointerfunctionscopyin];     [HashTable AddObject: @" Foo " ];     [HashTable AddObject: @" Bar " ];     [HashTable addobject:@  the ];     [HashTable removeobject: @" Bar " ];     NSLog (@ "members:%@"

The nshashtable is initialized based on an option parameter, because porting from OSX to the iOS platform, the enumeration type used on the OSX platform was discarded, and the name changed with option instead: Nshashtablestrongmemory: Equivalent to Nspointerfunctionsstrongmemory. A strong reference to a member variable. This is a default value. If you use this default value, Nshashtable and Nsset are no different. Nshashtableweakmemory: Equivalent to nspointerfunctionsweakmemory. A weak reference to a member variable. With Nspointerfunctionsweakmemory, the object reference is pointed to NULL when it is last released. Nshashtablezeroingweakmemory: Has been abandoned. Use Nshashtableweakmemory instead. Nshashtablecopyin: Replication before an object is added to the collection (nspointerfunction-acquirefunction). Equivalent to Nspointerfunctionscopyin. Nshashtableobjectpointerpersonality: Use pointers instead of actual values, which is equivalent to calling the description method when the pointer is printed. and the nspointerfunctionsobjectpointerpersonality equivalent. nsmaptableNsmaptable is a nsdictionary to the broader sense. Compared to nsdictionary/nsmutabledictionary, it has the following characteristics: * Nsdictionary/nsmutabledictionary copies keys and implements storage by strong reference to values. * The nsmaptable is variable. * Nsmaptable canThe keys and values are held by a weak reference, so when key or value is deallocated, the stored entity is removed。 * Nsmaptable can be copied and nshashtable similar to value when adding value, nsmaptable can store pointers arbitrarily and use the uniqueness of pointers to compare and repeat checks. usage:Suppose you use nsmaptable to store keys and referenced value without being copied., the value here is either a delegate or a weak type.
    ID Delegate= ...; Nsmaptable*maptable =[nsmaptable maptablewithkeyoptions:nsmaptablestrongmemory Valueoptio     Ns:nsmaptableweakmemory]; [Maptable setobject:DelegateForkey:@"Foo"]; NSLog (@"Keys:%@", [[Maptable Keyenumerator] allobjects]);     Similar to Nshashtable, Nsmaptable is also initialized according to an option. Nsmaptablestrongmemory nsmaptableweakmemory nshashtablezeroingweakmemory Nsmaptablecopyin NSMapTableObject Pointerpersonality subscripting
After reading the above few small examples, you might think, "Why not use object subscripting?". Some radical nshipster estimates have begun to write Nsmaptable's subscripting category. So why can't nsmaptable inherit subscripting?Take a look at the following code:
@implementation nsmaptable (nshipstersubscripting)-(ID) Objectforkeyedsubscript: (ID) key{     return  [self objectforkey:key];} -(void) SetObject: (ID) obj forkeyedsubscript: (ID) key{    if ( Obj! = nil) {        [self setobject:obj forkey:key]    ; Else {        [self removeobjectforkey:key];    }} @end
Note: The parameter key is of type。 This is very useful for nsdictionary nsmutabledictionary, but we cannot assume that the same applies to nsmaptable. We're stuck in an impasse: by ID, we can't use nsmaptable to implement subscripting. If the proxy method of object subscripting discards the constraint, then using nsmutabledictionary-setobject:forkeyedsubscript: The compile will not get the desired result. So to tell the truth, compared to the position of nsmaptable, the syntax of the convenience and speed is not the concern of the large number of people. In general, remember that programming is not about making people smarter, but about maximizing the ability to abstract a problem. Nsset and Nsdictionary are great classes that can solve 99% of problems and are undoubtedly the right tool to work with. If, however, your problem involves the memory problems mentioned above, nshashtable and nsmaptable are worth seeing. This article turns fromNidomThe blog, the original: "nshashtable & nsmaptable"Http://www.cocoachina.com/industry/20140605/8683.html

Nshashtable and Nsmaptable

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.