"Learning the path of iOS: Object-c" dictionary. Collection.

Source: Internet
Author: User

1. Immutable data dictionary (nsdicionary)

Dictionary: Used to store data with one by one correspondence.
A key corresponds to a value, the key plays a unique role, the key must be unique, but a vlaue can correspond to multiple keys.
Dictionaries store unordered elements, and a pair of key-value pairs make up an element in the dictionary.

①. Immutable Dictionary initialization Assignment

<span style= "FONT-SIZE:18PX;" >    //Laughter grammar  nsdictionary *dic = @{@ "name": @ "Zhangdan", @ "age": 18,@ "Gender": @ "man"};  Initialize  nsdictionary *dictionary = [[Nsdictionary alloc] Initwithobjectsandkeys:                 @ "Discard" @ "abandon" @ "Object" @ " Object ", @" index ", @" index ", nil];  NSLog (@ "%@", dictionary);   Traverse Constructor Method  Nsdictionary *dictionary1 = [nsdictionary dictionarywithobjectsandkeys:                           @ "Xiaozang", @ "name", @ " @ "Age" @ "23342432" @ "phone", nil];  NSLog (@ "%@", Dictionary1);</span>
②. Find the number of dictionary elements, that is, get all the keys and value
<span style= "FONT-SIZE:18PX;" >    //Get the number of elements   nsuinteger num = [Dictionary1 count];  NSLog (@ "%ld", num);   Get all key for   (NSString *temp in [Dictionary1 AllKeys]) {        NSLog (@ "%@", temp);     }   Nsarray  *array = [Dictionary1 AllKeys];    NSLog (@ "%@", array); Get the corresponding value nsstring *value = [Dictionary1 objectforkey:@ "age" according to key];</span>
③. Traversal dictionary: Get key, and value
<span style= "FONT-SIZE:18PX;" >     //Dictionary traversal: The key is obtained, then the corresponding value for      (NSString *temp in [Dictionary1 AllKeys]) {                 NSString *value = [ Dictionary1 Objectforkey:temp];                NSLog (@ "%@ =%@", temp,value);      } </span>
④. Some other operations
<span style= "FONT-SIZE:18PX;" >//1. Determine if it is the same as another dictionary-(BOOL) Isequaltodictionary: (Nsdictionary *) otherdictionary;//2. All the Vlaue in the dictionary are sorted by all keys, Use block syntax.-(Nsarray *) Keyssortedbyvalueusingcomparator: (nscomparator) cmptr</span>

2. Variable dictionary (nsmutabledictionary)

Variable dictionaries inherit immutable dictionaries, inheriting methods within immutable dictionaries, while adding deletions, additions, and modifications.

①. Creating a Dictionary object, initializing an assignment operation

<span style= "FONT-SIZE:18PX;" >  //Laughter grammar nsmutaledicrtionary *mut = [@{@ "name": @ "Zhangdan", @ "age": +} Mutablecopy];//Initialize  Nsmutabledictionary *mutable = [[Nsmutabledictionary alloc] init];   Convenient constructor method  nsmutabledictionary *mutable = [Nsmutabledictionary dictionarywithcapacity:1];  Convenience constructors Create object methods nsmutabledictionary *mutable1 = [nsmutabledictionary Dictionarywithobjectsandkeys: @ "@" @ "Age  " @ "Asdf", @ "name", Nil]; NSLog (@ "%@", mutable1); </span><p><span style= "font-size:18px;" ></span></p><span style= "FONT-SIZE:18PX;" ></span>

② to variable data dictionaries, adding, deleting, modifying elements
<span style= "FONT-SIZE:18PX;" >    //Add elements to the dictionary.  [mutable setobject:@ "forkey:@" "Age"];  NSLog (@ "%@", mutable);  Delete the corresponding element according to key  [mutable removeobjectforkey:@ "age"];  NSLog (@ "%@", mutable);  Modify the value corresponding to key  [mutable setobject:@ "1sad8" forkey:@ "Age"];    NSLog (@ "%@", mutable);  [mutable setvalue:@ "forkey:@" "Age"];  Remove all elements  [mutable removeallobjects];    Replace the original element in the dictionary with an element in another dictionary  [mutable setdictionary:otherdictionary];   Quickly remove the corresponding value according to key;  NSString *s1 = dictionary[@ "name"];//[dictionary objectforkey:@ "name"] NSLog (@ "%@", S1);  NSString *s2 = dictionary[@ "Age"]; NSLog (@ "%@", S2);</span>


3, set (Nsset and Nsmutableset)

1). Collection: unordered, cannot add duplicate elements

2). A mutable collection is a subclass of an immutable collection that contains all the methods of the parent class, while the Mutable collection adds the delete, modify, and add operations.

① common operations for immutable collections.

<span style= "FONT-SIZE:18PX;" >      //Initialization method   Nsset *set = [[Nsset alloc] initwithobjects:@ "AA", @ "DD", nil];      Convenient constructor method  Nsset *set = [[Nsset alloc] initwithobjects:@ "AA", @ "DD", Nil];<span style= "FONT-SIZE:18PX;" >  //number of elements nsuinteger num = [set count]; NSLog (@ "%ld", num); NSLog (@ "%@", set);   Gets the element NSLog in the collection (@ "%@", [set Anyobject]);   Fast traversal for (NSString *temp in Set) {NSLog (@ "%@", temp);}  Determines whether the collection contains an object  BOOL iscontain = [set Containsobject: @ "DD"];  Determine if two sets are the same  -(BOOL) Isequaltoset: (Nsset *) Otherset;  Determines whether the substring in the collection is the same as the incoming,  -(BOOL) Issubsetofset: (Nsset *) otherset;</span></span>



②. Variable Collection Operations
<span style= "FONT-SIZE:18PX;" >         //Create object      nsmutableset *set = [nsmutableset setwithobjects:@ "AA", @ "BB", @ "CC", @ "DD", nil];     Nsmutableset *set1 = [Nsmutableset setwithcapacity:1];<span style= "FONT-SIZE:18PX;" >    //Add     [Set addobject:@ "RRR"]; NSLog (@ "%@", set);     Delete   [set removeobject:@ "AA"]; NSLog (@ "%@", set);     Delete all     [set removeallobjects]; NSLog (@ "%@", set);    Adds an element from the collection, through the array     [set Addobjectsfromarray:array];     Replaces an element in a collection with an element of another collection    [set Setset:otherset];4.nscountedset</span></span><p><span style= "FONT-SIZE:18PX;" >    Nscountedset's parent class is Nsmutableset, so it inherits Nsset and Nsmutableset inside the method .</span></p>

Common operations

<span style= "FONT-SIZE:18PX;" >  Nscountedset *countset = [Nscountedset setwithobjects:                                        @ "AA", @ "BB", @ "CC", @ "DD", @ "ee", @ "AA", nil];  The number of repetitions in the array  nscountedset *count = [[Nscountedset alloc] initwithcapacity:1];  <span style= "FONT-SIZE:18PX;" >  Nsuinteger count = [countset countforobject:@ "AA"]; NSLog (@ "%ld", Count);</span></span>


adding elements
-(void) AddObject: (ID) object;
Move out of an object
-(void) Removeobject: (ID) object;

Example: There are 100 numbers, each value range is 20-55, to find out the number of repetitions,
the number of repetitions.
<span style= "FONT-SIZE:18PX;" >          Nscountedset *countset = [Nscountedset setwithcapacity:100];        for (int i = 0;i <; i++) {            int random = Arc4random ()% (55-22 + 1) +;            printf ("%d", random);            [Countset addobject:@ (Random)];        }                For (NSNumber *num in Countset) {          Nsuinteger count = [Countset countforobject:num];//records the number of repetitions.            if (Count > 1) {                NSLog (@ "%d, repeat%ld", [num intvalue],count);            }      } </span>

Summary: Dictionaries, collections. Arrays. The same and different points
<span style= "FONT-SIZE:18PX;"         >/** * * Same point: All belong to Collection (collection), used to store multiple objects, and do not restrict the type of object * different points: * 1. Function:         * Array-Managed Collection * Dictionary manages a collection of data sets that have one by one correspondence relationships that manage unordered and cross-specific elements.         * 2. Features * arrays: ordered, and elements can be duplicated.         * Dictionary: unordered, each element is a pair of key-value pairs, a key can only correspond to one value, but a value can correspond to more than one key,key is unique.         * Collection: unordered, cross-specific (elements cannot be duplicated, but the number of repetitions is recorded.)         * 3. Value method: * Array: The corresponding element is obtained by subscript.         * Dictionary: Gets the value of the pair using key.         * Collection: Random value. Anyobject, the efficiency of the collection is maximized.         * 4. Fast traversal: A for-in traversal cannot modify the traversed collection * array: The elements in the array when iterating out the dictionary: the key in the dictionary is traversed, and the corresponding value value is taken out by key.         * Collection: Iterates through the elements in the collection. * 5. Methods for creating objects 1. Initialization Method 2. Convenient constructor * Array: initwithobjects:arraywithobjects: * Dictionary: INI Twithobjectsandkeys:dictionarywithobjectsandkeys: * Set: initwithobjects:setwithobjects: */ </span>



"Learning the path of iOS: Object-c" dictionary. Collection.

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.