Nsdictionary&nsmutabledictionary Common Operation Carding

Source: Internet
Author: User
Tags delete key

The same array (Nsarray), the dictionary class Nsdictionary also supports object literals, which allows us to easily define initialization dictionary objects based on literal literals. The following is a literal syntax shortcut initialization dictionary (nsdictionary):

    nsdictionary* literaldictionary = @{@ "K1": @ "v1", @ "K2": @ "v2", @ "K3": @ "v3"};

1. Create initialization (initialization&creation) 1.1 Initializing an Dictionary(ns_designated_initializer)
-(instancetype) Init ns_designated_initializer;//based on va_list initialization nsdictionary-(instancetype) Initwithobjectsandkeys: ( ID) firstobject, ... ns_requires_nil_termination;//based on the keys array and corresponding values array initialization nsdictionary-(instancetype) initwithobjects: (Nsarray *) Objects Forkeys: (Nsarray *) keys;//Initializes a new dictionary (nsdictionary-) instancetype based on other initwithdictionary: ( Nsdictionary *) otherdictionary;-(instancetype) initwithdictionary: (nsdictionary *) otherdictionary CopyItems: (BOOL) Flag
1.2 Creating an Dictionary (autorelease)

+ (Instancetype) dictionary;//with a pair of Key-value initialize nsdictionary+ (instancetype) Dictionarywithobject: (ID) object ForKey: ( ID <NSCopying>) key;//initwithobjectsandkeys: Corresponding class static instantiation method + (Instancetype) Dictionarywithobjectsandkeys: (ID) Firstobject, ... ns_requires_nil_termination;//Initwithobjects:forkeys: Corresponding class static instantiation method + (Instancetype) dictionarywithobjects: (NSArray *) objects Forkeys: (Nsarray *) keys;//initwithdictionary: Corresponding class static instantiation method + (Instancetype) Dictionarywithdictionary: ( Nsdictionary *) Dict;

The following is a simple example:

    nsdictionary* QueryItemDict1 = [nsdictionary dictionarywithobjectsandkeys:@ "v1", @ "K1", @ "V2", @ "K2", @ "V3", @ "K3", nil] ;    NSLog (@ "QueryItemDict1 =%@", queryItemDict1);    nsdictionary* queryItemDict2 = [nsdictionary  dictionarywithobjects:@[@ "v1", @ "V2", @ "V3"] forkeys:@[@ "K1", @ "K2", @ "K3"];    NSLog (@ "queryItemDict2 =%@", queryItemDict2);

2. Accessing the dictionary (querying)number of 2.1 dictionary key-value pairs
@property (readonly) Nsuinteger count;

You can empty a dictionary based on Dictionary.count: if dictionary.count=0, it means that the dictionary is nil or contains no key-value pairs.

@property (readonly, copy) Nsarray *allkeys; An array of all keys @property (readonly, copy) Nsarray *allvalues; Array of all value

2.2 key value Index query

Find key corresponding to value (NSObject)-(ID) Objectforkey: (ID) akey;//is equivalent to Objectforkey, supports brackets subscript format (Dictionary[key]) to access the value of the specified key. -(ID) Objectforkeyedsubscript: (ID) key ns_available (10_8, 6_0);//Find all keys-with the same value (Nsarray *) Allkeysforobject: (ID) anobject;//finds the corresponding values array based on the keys array-(Nsarray *) Objectsforkeys: (Nsarray *) keys Notfoundmarker: (ID) marker;


3. Traversing the dictionary (enumerate)

-(void) Enumeratekeysandobjectsusingblock: (void (^) (id key, id obj, BOOL *stop)) block ns_available (10_6, 4_0);

-(Nsset *) Keysofentriespassingtest: (BOOL (^) (id key, id obj, BOOL *stop)) predicate ns_available (10_6, 4_0);


4. Dictionary sort (sorting)

Keyssortedbyvalueusingselector/keyssortedbyvalueusingcomparator by using the specified SEL or Nscomarator to the Keyarray to sort.

-(Nsarray *) Keyssortedbyvalueusingselector: (SEL) comparator;-(Nsarray *) Keyssortedbyvalueusingcomparator: ( Nscomparator) cmptr ns_available (10_6, 4_0);


5. Variable dictionary (nsmutabledictionary)5.1 Initializing an Dictionary(ns_designated_initializer)

In addition to inheriting Nsdictionary basic INIT, the following specified initialization functions have been added:

-(Instancetype) Initwithcapacity: (Nsuinteger) NumItems Ns_designated_initializer;
For example, the following code fragment gets the URL of the Queryitem Dictionary in the components query string:

    Http://weixin.sogou.com/weixin?type=2&ie=utf-8&query=NASA found New earth    nsstring* Wxnasaurlpath = @ "/http Weixin.sogou.com/weixin?type=2&ie=utf-8&query=nasa%e5%8f%91%e7%8e%b0%e6%96%b0%e5%9c%b0%e7%90%83 ";    nsurlcomponents* wxnasaurlcomponents =  [nsurlcomponents Componentswithstring:wxnasaurlpath];    nsmutabledictionary* queryitemdict = [nsmutabledictionary dictionary];    nsarray* queryitems = wxnasaurlcomponents.queryitems;    For (nsurlqueryitem* item in queryitems) {        [queryitemdict setObject:item.value forKey:item.name];    }    NSLog (@ "queryitemdict =%@", queryitemdict);


5.2 SetObject for key
Assignment substitution key-value pair-(void) SetObject: (ID) anobject forkey: (ID <NSCopying>) akey;//equivalent to Setobject:forkey:, Supports in-brackets subscript format (dictionary[key]=) assignment substitution. -(void) SetObject: (ID) obj forkeyedsubscript: (id <NSCopying>) key ns_available (10_8, 6_0);


5.3 Addentries & setdictionary
Append entries from Otherdictionary to current dictionary-(void) Addentriesfromdictionary: (Nsdictionary *) otherdictionary;// Equivalent to first removeallobjects after Addobjectsfromarray;setdictionary is similar to an assignment (setter) to retain propery. -(void) Setdictionary: (Nsdictionary *) otherdictionary;


5.4 Removeobject
Delete key-value pair-(void) Removeobjectforkey: (ID) of the specified key akey;//delete the corresponding key-value pair for the specified key array-(void) Removeobjectsforkeys: (Nsarray *) keyarray;//Delete clears all key-value pairs-(void) removeallobjects;


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nsdictionary&nsmutabledictionary Common Operation Carding

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.