OBJECTIVE-C (Foundation framework One nsdictionary)

Source: Internet
Author: User

Nsdictionary

Immutable dictionaries

Ways to create a dictionary

        How to create a dictionary        nsdictionary *dy = [Nsdictionary dictionarywithobject:@ "a" forkey:@ "B"];        Nsdictionary *dy1 = [Nsdictionary dictionarywithobjectsandkeys:@ "a", @ "B", @ "C", @ "D", nil];                Quick Create dictionary        //duplicate key value, duplicate cannot save to dictionary        nsdictionary *dy2 = @{@ "we": @ "haha", @ "we": @ "Ha"};                NSLog (@ "%@", dy);        NSLog (@ "%@", dy1);        NSLog (@ "%@", Dy2);

Get the length of the dictionary

        //Get dictionary lengthNsdictionary *dy2 = @{@"We":@"haha",@"W":@"ha"}; NSLog (@"%lu", Dy2.count);//Output 2//if the key value is repeated, the length is not countedNsdictionary *dy2 = @{@"We":@"haha",@"We":@"ha"}; NSLog (@"%lu", Dy2.count);//Output 1

The traversal of a dictionary

Nsdictionary *dy1 = [Nsdictionary Dictionarywithobjectsandkeys:@"a",@"b",@"C",@"D", nil]; //quickly create a dictionary//Duplicate key value, duplicate cannot be saved to dictionaryNsdictionary *dy2 = @{@"We":@"haha",@"W":@"ha"}; NSLog (@"%lu", Dy2.count); //the traversal of a dictionary         for(NSString *keyinchdy2) {NSLog (@"key =%@, value =%@", Key, [Dy2 Objectforkey:key]); }        //Enumeration Type Traversal[Dy1 enumeratekeysandobjectsusingblock:^ (IDKeyIDobj, BOOL *stop) {NSLog (@"key =%@, value =%@", key, obj); }];

Save the dictionary in a file

        //Save the dictionary to a fileNsdictionary *dy2 = @{@"We":@"haha",@"W":@"ha"}; BOOL Iswrite= [Dy2 writetofile:@"/users/cloudwalk/desktop/test.plist"Atomically:yes]; if(iswrite) {NSLog (@"Write Success"); }

Reading a dictionary from a file

        Nsdictionary *readdy2 = [nsdictionary dictionarywithcontentsoffile:@ "/users/cloudwalk/desktop/ Test.plist"];        NSLog (@ "%@", readDy2);        

Build an array dictionary by adding arrays to the dictionary

Nsarray *sdarr = [Nsarray arraywithobjects:@"Zaozhuang",@"Jinan", nil]; Nsarray*jxarr = [Nsarray arraywithobjects:@"Jiujiang",@"Nanchang", nil]; Nsdictionary*citys = [Nsdictionary Dictionarywithobjectsandkeys:sdarr,@"SD", Jxarr,@"JX", nil]; NSLog (@"citys =%@", citys);

        Nsdictionary *citys = [Nsdictionary Dictionarywithobjectsandkeys:sdarr,@ "SD", Jxarr,  @ "jx", nil];        NSLog (@ "citys =%@", citys);         // Save the Citys in        a file [Citys writetofile:@ "/users/cloudwalk/desktop/test1.plist" Atomically:yes];

OBJECTIVE-C (Foundation framework One nsdictionary)

Related Article

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.