Common methods for Nsdictionary (dictionary), nsmutabledictionary (variable dictionary), Nsset (set), Nsmutableset (mutable collection) in OC

Source: Internet
Author: User
Tags allkeys delete key

dictionaries are used to hold collections with mapped relational dataa key-value is considered an entry (entry), a dictionary is a container for storing key-value pairs.Unlike arrays, dictionaries access elements by keykey cannot be duplicated, value must be an objectKey-value pairs are stored in the dictionary as unorderedDictionary points: immutable dictionaries (nsdictionary) and variable dictionaries (nsmutabledictionary)Immutable Dictionaries once created, key value pairs cannot be changed, cannot be added, cannot be deleted, can only read key or valueCommon methods are:1. Create a Dictionary object2. Get all key values, get all value values3. Query value by key value1. Commonly used methods of common dictionariesYou need to assign key-value pairs when you create a Dictionary object, but in order: value, key, (value in the back of the previous key)nsdictionary *dic1 = [[Nsdictionary alloc] initwithobjectsandkeys:@ "Duke", @ "name", @33,@ "age", @ "male", @ "gender", nil ];
NSLog (@ "%@", Dic1);
nsdictionary *dic2 = [nsdictionary dictionarywithobjectsandkeys:@ "Duke", @ "name", @33,@ "age", @ "male" @ "gender", nil] ;
NSLog (@ "%@", dic2);
Nsarray *keys = @[@ "name" @ "Age" @ "gender"];Nsarray *values = @[@ "Duke", @33,@ "male"];the grammatical sugar form of a dictionaryKey-value pairs are separated by commas, and the keys and values are separated by colonsnsdictionary *dic5 = @{@ "name": @ "Duke" @ "Age": @33,@ "Gender": @ "male"};NSLog (@ "%@", dic5);The number of two array elements must be identical when creating a Dictionary object nsdictionary *dic3 = [[Nsdictionary alloc] initwithobjects:values Forkeys:keys];
NSLog (@ "%@", dic3);
nsdictionary *dic4 = [nsdictionary dictionarywithobjects:values Forkeys:keys];NSLog (@ "%@", dic4);get the number of key-value pairs in a dictionary by using the Count method Nsinteger count = [dic4 count]; NSLog (@ "%ld", count);get all the keys in the dictionaryNsarray *allkeys = [Dic4 AllKeys];NSLog (@ "%@", AllKeys);gets a value that consists of all the values in the dictionary Nsarray *allvalues = [Dic4 allvalues]; NSLog (@ "%@", allvalues);gets its corresponding value in the dictionary by the specified key id object = [dic4 objectforkey:@ "age"];
NSLog (@ "%@", object);

for (int i = 0; i < count; i++) {
id key = [allKeys objectatindex:i];gets the corresponding value based on the key currently being traversedid value = [dic4 Objectforkey:key];
nsstring *result = [value iskindofclass:[nsstring class]]? @ "YES": @ "NO";
NSLog (@ "%@:%@-->%@", Key,value,result);    }nsmutabledictionary Variable Dictionaryvariable nsmutabledictionary is a subclass of Nsdictionary, with all nsdictionary methodsCommon methods are:1. Create a Dictionary object2. Add key value pairs3, change the value of the key corresponding4. Delete key value pairs5, through the Fou loop to traverse all key value pairsnsmutabledictionary creation of variable Dictionary objectsnsmutabledictionary *dic6 = [[Nsmutabledictionary alloc] initwithdictionary:dic5];
NSLog (@ "%@", dic6);
nsmutabledictionary *dic7 = [Nsmutabledictionary dictionarywithdictionary:dic5];
NSLog (@ "%@", dic7);
nsmutabledictionary *dic8 = [[Nsmutabledictionary alloc] init];
NSLog (@ "%@", Dic8);
nsmutabledictionary *dic9 = [nsmutabledictionary dictionary];NSLog (@ "%@", dic9);increase key-value pairs [dic9 setobject:@ "Duke" forkey:@ "name"];
[dic9 setobject:@ "male" forkey:@ "gender"];
[dic9 setobject:@33 forkey:@ "age"];
NSLog (@ "%@", dic9);to modify a value that corresponds to an existing keyif the key does not exist, a key-value pair is added, and if the key exists, the value corresponding to the existing key is modified [dic9 setobject:@34 forkey:@ "age"];
NSLog (@ "%@", dic9);deletes the corresponding key-value pair according to the specified key [dic9 removeobjectforkey:@ "age"];
NSLog (@ "%@", dic9);Remove all key-value pairs [dic9 removeallobjects];
NSLog (@ "%@", dic9);the set in OC (Nsset) is the same as a collection in mathematics, where elements in a collection are unique, elements of a cell are unordered, and storage elements must be object typesSet representation set in OC, divided into Nsset and Nsmutablesetcreation of Nsset objects is unordered, cross-specificcommon methods of Nsset1. Create a Collection Object2. Get the number of elements3. Get an element in the collection4. Determine if an object is contained in the collectionTo create a collection object Nsset *set1 = [[Nsset alloc] initwithobjects:@ "1", @ "2", @ "2", @ "3", nil]; NSLog (@ "%@", Set1);
nsset *set2 = [Nsset setwithobjects:@ "3", @ "2", @ "1", nil];
NSLog (@ "%@", Set2);to create a collection object with an array objectYou can filter out duplicate element objects in the array by this methodNsarray *array = @[@1,@2,@3,@2,@3];
nsset *set3 = [[Nsset alloc] initwitharray:array];
nsset *set4 = [Nsset Setwitharray:array];
NSLog (@ "%@", Set3);
NSLog (@ "%@", set4);
gets the number of objects in the collection Nsinteger count2 = [Set4 count];
NSLog (@ "%ld", count2);
gets the objects in the collection id object1 = [set4 anyobject];
NSLog (@ "%@", Object1);
determines whether a given object is contained in the specified collection NSString *result1 = [Set4 containsobject:@3]? @ "YES": @ "NO";
NSLog (@ "% @is contained in set%@", @3,result1);common methods of Nsmutableset1. Create a Collection Object2. Adding elements3. Deleting elementsCreate a Nsmutableset objectnsmutableset *mutableset1 = [[Nsmutableset alloc] init];
NSLog (@ "%@", MutableSet1);
nsmutableset *mutableset2 = [Nsmutableset set];
NSLog (@ "%@", MutableSet2);creating with immutable objects Nsmutableset *mutableset3 = [[Nsmutableset alloc] initwithset:set1];
NSLog (@ "%@", MutableSet3);
nsmutableset *mutableset4 = [Nsmutableset Setwithset:set1];NSLog (@ "%@", mutableSet4);adding a collection element [MutableSet4 addobject:@4]; NSLog (@ "%@", mutableSet4);Delete a single collection [MutableSet4 removeobject:@ "3"]; NSLog (@ "%@", mutableSet4);Delete all elements [MutableSet4 removeallobjects]; NSLog (@ "%@", mutableSet4);NscountedsetNscountedset is a subclass of Nsmutablestringability to record the number of repetitions of an elementadded a Count function based on setNscountedset Record the number of occurrences of the collection object added Nscountedset *countedset = [Nscountedset set];
[Countedset addobject:@1];
[Countedset addobject:@2];
[Countedset addobject:@2];
NSLog (@ "%@", countedset);gets the number of times an object has occurred in the collection individually Nsinteger countofobjec = [Countedset countforobject:@1]; NSLog (@ "%ld", COUNTOFOBJEC); iterating through an array element with a quick enumeration Nsarray *testarray = @[@1,@2,@3,@4,@5];   For (ID object in Testarray) {
NSLog (@ "%@", object);
    }
For (NSNumber *object in Testarray) {
NSLog (@ "%@", object);
    }fast traversal of collectionsFor (ID object in Set1) {
NSLog (@ "%@", object);
    }
quickly traverse a dictionaryDirect traversal of the dictionary directly gets the dictionary of each key, you can iterate through the resulting key to get the corresponding valueFor (NSString *key in Dic1) {
NSLog (@ "dictionary[%@]:%@", Key,dic1[key]);
    }
//Dic1[key] is equivalent to [Dic1 Objectforkey:key]

 

Common methods for Nsdictionary (dictionary), nsmutabledictionary (variable dictionary), Nsset (set), Nsmutableset (mutable collection) in OC

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.