iOS development OC (20)--foundation (5) nsdictionary

Source: Internet
Author: User

/*

Collection

1.nsarray\nsmutablearray

• Orderly

• fast Creation (immutable): @[obj1, Obj2, Obj3]

• Quick access to elements: array name [i]

2.nsset\nsmutableset

• unordered

3.nsdictionary\nsmutabledictionary

• unordered

• fast Creation (immutable): @{key1:value1, key2:value2}

• Quick access to elements: dictionary name [key]

*/

Nsdictionary: Dictionary

code example

1>

voidCreate () {/*dictionary: Key----> Value Index----> The contents stored in the text content are keys .*/    //How dictionaries are created//nsdictionary *dict = [nsdictionary dictionarywithobject:@ "Jack" forkey:@ "name"]; //Nsarray *keys = @[@ "name" @ "Address"]; //Nsarray *objects = @[@ "Jack", @ "Beijing"]; //nsdictionary *dict = [nsdictionary dictionarywithobjects:objects Forkeys:keys];       /*nsdictionary *dict = [nsdictionary Dictionarywithobjectsandkeys: @ "Jack" @ "name" @ "Beijing" @ "address", @ "32423434", @ "QQ", nil];*/        //The most practical wayNsdictionary *dict = @{@"name":@"Jack",@"Address":@"Beijing"}; //id obj = [dict objectforkey:@ "name"]; //Get Value    IDobj = dict[@"name"]; NSLog (@"%@", obj); //returns the number of key-value pairsNSLog (@"%ld", Dict.count);}

2> Basic Use

voiduse1 () {nsmutabledictionary*dict =[Nsmutabledictionary dictionary]; //to add a key-value pair[Dict setobject:@"Jack"Forkey:@"name"]; [Dict setobject:@"Beijing"Forkey:@"Address"]; [Dict setobject:@"Rose"Forkey:@"name"]; //removing key-value pairs//[dict removeobjectforkey:<# (ID) #>];NSString*str = dict[@"name"]; //NSLog (@ "%@", str);NSLog (@"%@", Dict); //NSLog (@ "%@", @[@ "Jack", @ "Rose"]);}voidUse2 () {nsmutabledictionary*dict = @{@"name":@"Jack"}; [Dict setobject:@"Rose"Forkey:@"name"];}voiduse3 () {//The dictionary does not allow the same key, but allows the same value (Object)//The disordered of the dictionaryNsdictionary *dict = @{    @"Address":@"Beijing",    @"name":@"Jack",    @"name2":@"Jack",    @"Name3":@"Jack",    @"QQ":@"7657567765"}; //Nsarray *keys = [Dict AllKeys]; //    //for (int i = 0; i<dict.count; i++)//    {    //nsstring *key = keys[i]; //nsstring *object = Dict[key]; //    //    //NSLog (@ "%@ =%@", key, object); //    }     //use block to access[dict enumeratekeysandobjectsusingblock:^(IDKeyIDobj, BOOL *stop) {NSLog (@"%@ - %@", key, obj); //*stop = YES;     }];}
#import<Foundation/Foundation.h>intMain () {Nsarray*persons = @[    @{@"name":@"Jack",@"QQ":@"432423423",@"Books": @[@"5 minutes of iOS programming",@"5 minutes to break Android programming"]},    @{@"name":@"Rose",@"QQ":@"767567"},    @{@"name":@"Jim",@"QQ":@"423423"},    @{@"name":@"Jake",@"QQ":@"123123213"}    ]; //nsdictionary *jim = persons[2];NSString*bookname = persons[0][@"Books"][1]; NSLog (@"%@", BookName); //Nsarray *array = persons[0][@ "Books"]; //NSLog (@ "%@", array); //Remove the 1-bit dictionary first//and then take out the dictionary of QQ this key corresponding data//NSLog (@ "%@", persons[1][@ "QQ"]); //NSLog (@ "%@", Jim);    return 0;}

The code is in the previous study, according to Li Mingjie teacher's video knock, again, thanked him, Li said very well, recommend everyone to see, he is not the programming, but a thought, let me benefit.

iOS development OC (20)--foundation (5) nsdictionary

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.