1 /**2 in the OC in Java3 Nsarray List4 Nsset Set5 nsdictionary Map6 7 Collection8 1.nsarray\nsmutablearray9 * OrderlyTen * Fast Creation (immutable): @[]; One * Quick access element: array name [i]; A - 2.nsset\nsmutableset - * Unordered the - 3.nsdictionary\nsmutablensmutable - * Unordered - * Fast Creation (immutable): @{key1:value1, key2:value2} + * Quick access element: Dictionary name [key]; - */ + A at #import<Foundation/Foundation.h> - - intMainintargcConst Char*argv[]) { - @autoreleasepool { - - /** in Key ---> value value pairs - Index---> Text content to */ + /** How to create a dictionary 1*/ -Nsdictionary *dict1 = [Nsdictionary dictionarywithobject:@"Chris Paul"Forkey:@"name"]; the IDObj1 = [Dict1 objectforkey:@"name"]; *NSLog (@"%@", obj1); $ Panax Notoginseng /** How to create a dictionary 2*/ -Nsarray *keys1 = @[@"name",@"Address"]; theNsarray *objects = @[@"Chris Paul",@"Los Angeles"]; +Nsdictionary *dict2 =[Nsdictionary dictionarywithobjects:objects forkeys:keys1]; A IDObj2 = [Dict2 objectforkey:@"Address"]; theNSLog (@"%@", obj2); + - /** How to create a dictionary 3*/ $Nsdictionary *dict3 = [Nsdictionary Dictionarywithobjectsandkeys:@"Chris Paul",@"name",@"Los Angeles",@"Address", nil]; $ IDOBJ3 = [Dict3 objectforkey:@"name"]; -NSLog (@"%@", obj3); - the /** Dictionary Creation Method 4: Quick Create (Xcode new feature)*/ -Nsdictionary *dict4 = @{@"name":@"Chris Paul",@"Address":@"Los Angeles"};/** @{@ "ID objec": ID, ...} The return value is Nsdictionary*/Wuyi IDObj4 = dict4[@"Address"];/** This method is also a new feature of Xcode*/ theNSLog (@"%@", OBJ4); - Wu /** Access to Dictionary key-value pairs*/ -NSLog (@"%ld", dict4.count); About $ /** Creation of variable dictionaries*/ -Nsmutabledictionary *dict5 =[Nsmutabledictionary dictionary]; -[Dict5 SetObject:@"Chris Paul"Forkey:@"name"];/** Add key-value pairs*/ -[Dict5 SetObject:@"Los Angeles"Forkey:@"Address"];/** Add key-value pairs*/ A[Dict5 SetObject:@"Magic Johnson"Forkey:@"name"];/** This time, Chris Paul will be covered*/ +NSLog (@"%@", dict5[@"name"]); the[Dict5 Removeobjectforkey:@"Address"];/** Remove Key-value pairs*/ -NSLog (@"%@", dict5[@"Address"]); $ the /** Dictionary Traversal Method 1*/ theNsdictionary *DICT6 = @{@"name":@"Chris Paul", the @"Address":@"Los Angeles", the @"name2":@"Chris Paul"}; - /** Note: in 1. The dictionary does not allow the same key, but allows the same value (object) the 2. The dictionary is unordered the */ AboutNsarray *keys2 = [Dict6 AllKeys];/** This method prints out is messy*/ theNSLog (@"%@", keys2); the for(inti =0; i<dict6.count; i++) { theNSString *key =Keys2[i]; +NSString *Object=Dict6[keys2]; -NSLog (@"%@---%@", Key,Object); the }Bayi the /** Dictionary traversal method 2:block*/ the[Dict6 enumeratekeysandobjectsusingblock:^ (IDKeyIDobj, BOOL *stop) { -NSLog (@"%@---%@", key, obj); - }]; the } the return 0; the}
The use of the Nsdictionary of Foundation framework