Main.m
////MAIN.M//Nsdictionary////Created by Zhaogang on 16/5/8.//Copyright 2016 Zhaogang. All rights reserved./// * Collection Nsarray ArrayList nsset HashSet nsdictionary Map 1. Nsarray\nsmutablearray * ordered * fast Creation (immutable): @[] * Quick access element: array name [] 2. Nsset\nsmutableset * Unordered 3. Nsdictionary\nsmutabledictionary * unordered * Quick Create (immutable): @{key:value} * Quick access: dictionary name [key] */#import <Foundation/Foundation.h> intMainintargcConst Char* argv[]) {Nsarray*person = @[@{@"Address": @"Beijing", @"Name": @"Jack", @"Age": @"a"}, @{@"Address": @"Shanghai", @"Name": @"Rose", @"Age": @"very"}, @{@"Address": @"Nanjing", @"Name": @"Mike.", @"Age": @"All"}, @{@"Address": @"Hongkong", @"Name": @"Hobe", @"Age": @"very"} ];//Nsdictionary *jack = person[0];//NSLog (@ "%@", Jack); NSLog(@"%@", person[1][@"Address"]);return 0;}voidTest3 () {//dictionary does not allow a key to be used, but allows the same value (object) nsdictionary*dict = @{@"Address": @"Beijing", @"Name": @"Jack", @"Age": @"a"};//Nsarray *keys = [Dict AllKeys]; //for (int i = 0; i < Dict.count; i++) // { ////[keys objectatindex:i]; //NSString *key = keys[i]; //NSString *object = Dict[key]; // //NSLog (@ "%@-%@", key, object); // }[Dict enumeratekeysandobjectsusingblock:^ (ID_nonnull Key,ID_nonnull obj,BOOL* _nonnull stop) {NSLog(@"%@ - %@", key, obj);//*stop = YES;}];}voidTest2 () {//Error //Nsmutabledictionary *dict = @{@ "name": @ "Jack"}; // //[Dict setobject:@ "Rose" forkey:@ "name"]; nsmutabledictionary*dict = [nsmutabledictionaryDictionary];//Add key-value pairs, no order[Dict setobject:@"Jack"forkey:@"Name"]; [Dict setobject:@"Beijing"forkey:@"Address"];//This will overwrite the value of the key above[Dict setobject:@"Rose"forkey:@"Name"];//Remove key-value pairs //[dict removeobjectforkey:<# (nonnull ID) #>]; NSString*str = dict[@"Name"];NSLog(@"%@", str);//nslog (@ "%@", @[@ "Jack", @ "Rose"]); NSLog(@"%@", dict);}voidTest () {/* Dictionary: Key---->value index----> the contents stored in the text are key-value pairs . //nsdictionary *dict = [nsdictionary dictionarywithobject:@ "Jack" forkey:@ "name"]; //Nsarray *key = @[@ "name", @ "address"]; //Nsarray *object = @[@ "Jack", @ "Beijing"]; // //Nsdictionary *dict = [nsdictionary dictionarywithobjects:object forkeys:key]; //Nsdictionary *dict = [nsdictionary dictionarywithobjectsandkeys: //@ "Jack", @ "name", //@ "Beijing", @ "address", //@ "a", @ "age", nil]; nsdictionary*dict = @{@"Name": @"Jack", @"Address": @"Beijing", @"Age": @"a"};IDobj = [Dict objectforkey:@"Age"];NSLog(@"%@", obj);//Returns the number of key-value teams NSLog(@"%ld", Dict. Count);}
A brief introduction to OBJECT-C------nsdictionary