Objective-c:foundation Framework-Common class-nsdictionary

Source: Internet
Author: User

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

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.