A dictionary is a collection of keywords and their definitions (descriptions). The collection of implementation dictionaries in cocoa Nsdictionary stores a numeric value (which can be any type of object) under a given keyword (usually a nsstring). Then you can use this keyword to find the corresponding value.
Unlike arrays, dictionaries (also known as hash lists or associative arrays) use an optimized storage method for key queries. It can immediately find the data to be queried without having to traverse the entire array to find it.
You can use Dictionarywithobjectsandkeys to create a dictionary
Query Dictionary value: Objectforkey
Nsmutabledictionary's Dictionary method can either create a mutable dictionary or use the Dictionarywithcapaticy:.
Use the Setobject:forkey: method to add a dictionary element and replace the old value with a new implant if the keyword already exists.
Similarly, the Nsmutabledictionary class allows you to arbitrarily add or remove dictionary elements.
add element: Setobject:forkey:
Delete element: Removeobjectforkey:
#import <Foundation/Foundation.h>@interface myclass:nsobject{ *firstName; *lastName;} -(void) Setfirstname: (nsstring*) m_firstname; -(nsstring*) firstName; -(void) Setlastname: (nsstring*) m_lastname; -(nsstring*) lastName; @end
////MYCLASS.M//Dictionary////Created by ARON-MAC-VM on 3/14/16.//Copyright (c) pqlabs. All rights reserved.//#import"MyClass.h"@implementation MyClass-(void) Setfirstname: (NSString *) m_firstname{FirstName=M_firstname;}-(nsstring*) firstname{returnFirstName;}-(void) Setlastname: (NSString *) m_lastname{LastName=M_lastname;}-(nsstring*) lastname{returnLastName;}-(nsstring*) description{if(FirstName = = Nil | | lastName = =Nil) { return @"No Name found."; } Else { return[NSString stringWithFormat:@"%@ %@", Firstname,lastname]; }} @end
////main.m//Dictionary////Created by ARON-MAC-VM on 3/14/16.//Copyright (c) pqlabs. All rights reserved.//#import<Foundation/Foundation.h>#import"MyClass.h"intMainintargcConst Char*argv[]) {@autoreleasepool {MyClass*my1 = [MyClassNew]; MyClass*my2 =[[MyClass alloc]init]; MyClass*my3 = [MyClassNew]; MyClass*my4 = [MyClassNew]; Nsdictionary*myclassdict; Myclassdict= [Nsdictionary dictionarywithobjectsandkeys:my1,@"my1", My2,@"My2", My3,@"my3", My4,@"My4", nil]; //Get value ObjectforkeyMyClass *sub = [myclassdict objectforkey:@"my3"]; if(sub==Nil) {Exit (1); } [Sub Setfirstname:@"Pan"]; [Sub Setlastname:@"Sunde"]; NSLog (@"mofiy The data:%@", sub); //Traverse Dictionary for(ID keyinchmyclassdict) {NSLog (@"key:%@, Value:%@", Key,[myclassdict Objectforkey:key]); } //Add new Element//Nsdictionary cannot add or remove elements, you can use Nsmutabledictionarynsmutabledictionary*mynewdict =[Nsmutabledictionary dictionary]; //Add an existing dictionary to a pair of elements in a new dictionary[Mynewdict setobject:myclassdict Forkey:@"The old immutable dictionary myclassdict"]; //traverse to add Existing data (original dictionary) for(ID keyinchmyclassdict) {[Mynewdict setobject:[myclassdict Objectforkey:key] forkey:key]; } nsstring*newvalue =@"This is a new Value"; NSString*newkey =@"a new Key"; [Mynewdict Setobject:newvalue Forkey:newkey]; //Traverse MynewdictNSLog (@"The ********* traversal dictionary mynewdict as follows:"); for(ID keyinchmynewdict) {NSLog (@"Key:%@, Value:%@", Key,[mynewdict Objectforkey:key]); } NSLog (@"********* Traversal dictionary mynewdict over:"); //Delete Element[mynewdict Removeobjectforkey:@"a new Key"]; for(ID keyinchmynewdict) {NSLog (@"key:%@,value:%@", Key,[mynewdict Objectforkey:key]); } } return 0;}
Dictionary: Examples of nsdictionary applications