Like NSString, Nsarray, Nsdictionary is immutable, and its corresponding variable type is nsmutabledictionary. Its usage is as follows:
#import<Foundation/Foundation.h>@interfaceStudent:nsobject@property (nonatomic, retain) nsstring*name;+ (ID) Studentwithname: (NSString *) name;@end#import "Student.h"@implementationStudent+ (ID) Studentwithname: (NSString *) name {Student*stu =[[Student alloc] init]; Stu.name=name; return[Stu Autorelease];}- (void) dealloc {NSLog (@"%@ was destroyed.", _name); //Release Name[_name release]; [Super Dealloc];}@end
#import<Foundation/Foundation.h>#import "Student.h"#pragmaThe initialization of the Mark dictionaryvoiddictcreate () {//nsdictionary is immutable.Nsdictionary *dict = [Nsdictionary dictionarywithobject:@"v"Forkey:@"k"]; //The most common way to initializeDict =[nsdictionary Dictionarywithobjectsandkeys:@"v1",@"K1", @"v2",@"K2", @"v3",@"K3", nil]; Nsarray*objects = [Nsarray arraywithobjects:@"v1",@"v2",@"v3", nil]; Nsarray*keys = [Nsarray arraywithobjects:@"K1",@"K2",@"K3", nil]; Dict=[Nsdictionary dictionarywithobjects:objects Forkeys:keys]; NSLog (@"%@", dict);}#pragmaThe basic usage of the Mark dictionaryvoidDictuse () {nsdictionary*dict =[nsdictionary Dictionarywithobjectsandkeys:@"v1",@"K1", @"v2",@"K2", @"v3",@"K3", nil]; //count is how many key-value pairs are computed (key-value)NSLog (@"Count=%zi", Dict.count); //since nsdictionary is immutable, you can only take values and not modify values IDobj = [Dict objectforkey:@"K2"]; NSLog (@"obj=%@", obj); //writing a dictionary to a fileNSString *path =@"/users/apple/desktop/dict.xml"; [Dict Writetofile:path Atomically:yes]; //read content from a fileDict =[Nsdictionary Dictionarywithcontentsoffile:path]; NSLog (@"dict=%@", dict);}#pragmaThe use of the Mark dictionaryvoidDictUse2 () {nsdictionary*dict =[nsdictionary Dictionarywithobjectsandkeys:@"v1",@"K1", @"v2",@"K2", @"v3",@"K3", nil]; //back to all keysNsarray *keys =[Dict AllKeys]; //NSLog (@ "keys=%@", keys);Nsarray*objects =[Dict allvalues]; //NSLog (@ "objects=%@", objects); //remove multiple value corresponding to multiple keys//when key cannot find the corresponding value, use the marker parameter value insteadobjects = [Dict objectsforkeys:[nsarray arraywithobjects:@"K1",@"K2",@"K4", Nil] Notfoundmarker:@"Not-found"]; NSLog (@"objects=%@", objects);}#pragmaMark Traversal Dictionaryvoiddictfor () {nsdictionary*dict =[nsdictionary Dictionarywithobjectsandkeys:@"v1",@"K1", @"v2",@"K2", @"v3",@"K3", nil]; //traverse all keys of a dictionary for(IDKeyinchdict) { IDValue =[Dict Objectforkey:key]; NSLog (@"%@=%@", key, value); }}#pragmaMark Traversal Dictionary 2voidDictFor2 () {nsdictionary*dict =[nsdictionary Dictionarywithobjectsandkeys:@"v1",@"K1", @"v2",@"K2", @"v3",@"K3", nil]; //Key IteratorNsenumerator *enumer =[Dict Keyenumerator]; IDKey =Nil; while(key =[Enumer Nextobject]) { IDValue =[Dict Objectforkey:key]; NSLog (@"%@=%@", key, value); } //Object Iterator//[Dict objectenumerator];}#pragmaMark Traversal Dictionary 3voidDictFor3 () {nsdictionary*dict =[nsdictionary Dictionarywithobjectsandkeys:@"v1",@"K1", @"v2",@"K2", @"v3",@"K3", nil]; [Dict Enumeratekeysandobjectsusingblock:^(IDKeyIDobj, BOOL *stop) {NSLog (@"%@=%@", key, obj); }];}#pragmaMarkvoiddictmemory () {Student*STU1 = [Student studentwithname:@"STU1"]; Student*STU2 = [Student studentwithname:@"STU2"]; Student*STU3 = [Student studentwithname:@"STU3"]; //when an object is called a dictionary key or value, a retain operation is done, that is, the counter is +1Nsdictionary *dict =[Nsdictionary dictionarywithobjectsandkeys:stu1,@"K1", STU2,@"K2", STU3,@"K3", nil]; //when the dictionary is destroyed, all the key and value inside will do a release operation, which is the counter-1}
Objective-c:foundation Framework-Common class-nsdictionary