NSDictionary Example in Objective C
If you have not read my last tutorial Please read it from here:
Learn Objective C
Before going to this tutorial think about dictionary. What is dictionary ?. A Dictionary is collection of words arranged alphabetically.Similarly in computer science of Objective C programming NSDictionary is the collection of words like an array but arranged with keys so that easy to find.
NSDictionary vs. NSArray
If there are total 5000 entry in array then it is very difficult to remember the index of each element in an array. But in NSDicitionary it is easy to remember just keys.In other words in NSDicitionary There is user defined index.
Syntax to declare NSDictionary
NSDictionary *dictionary=@{@"key1":@"student1",@"key2":@"student2"};
how to access value from nsdictionary in objective c
The value from NSDictionary in objedctive c is accessed from key which acts like index.
Example: get=dictionary[@"key1"];
Similarly NSDictionary has two types: NSDictionary and NSMutableDictionary.
I have used only NSDictionary Class here you can add NSMutableDictionary class accordingly if you have not read my last NSArray Tutorial you can read from here: NSArray in Objective C
Example:
It is very easy example. First open command line project in Xcode and after that add Student class to the project.
In Student.h
@interface Student : NSObject
{
NSDictionary *studentNameDictionary;
}
-(void)getStudentName;
-(void)printStudentName;
In Student.m
@implementation Student
-(void)getStudentName
{
studentNameDictionary=@{@"st1":@"abc",@"st2":@"def",@"st3":@"ghi",@"st4":@"jkl"};
}
-(void)printStudentName
{
NSLog(@"The values appear in dictionary are:\n %@ \n %@ \n %@",studentNameDictionary[@"st2"],studentNameDictionary[@"st1"],studentNameDictionary[@"st4"],studentNameDictionary[@"st3"]);
}
If you have not read my last tutorial Please read it from here:
Learn Objective C
Before going to this tutorial think about dictionary. What is dictionary ?. A Dictionary is collection of words arranged alphabetically.Similarly in computer science of Objective C programming NSDictionary is the collection of words like an array but arranged with keys so that easy to find.
NSDictionary vs. NSArray
If there are total 5000 entry in array then it is very difficult to remember the index of each element in an array. But in NSDicitionary it is easy to remember just keys.In other words in NSDicitionary There is user defined index.
Syntax to declare NSDictionary
NSDictionary *dictionary=@{@"key1":@"student1",@"key2":@"student2"};
how to access value from nsdictionary in objective c
The value from NSDictionary in objedctive c is accessed from key which acts like index.
Example: get=dictionary[@"key1"];
Similarly NSDictionary has two types: NSDictionary and NSMutableDictionary.
I have used only NSDictionary Class here you can add NSMutableDictionary class accordingly if you have not read my last NSArray Tutorial you can read from here: NSArray in Objective C
Example:
It is very easy example. First open command line project in Xcode and after that add Student class to the project.
In Student.h
@interface Student : NSObject
{
NSDictionary *studentNameDictionary;
}
-(void)getStudentName;
-(void)printStudentName;
In Student.m
@implementation Student
-(void)getStudentName
{
studentNameDictionary=@{@"st1":@"abc",@"st2":@"def",@"st3":@"ghi",@"st4":@"jkl"};
}
-(void)printStudentName
{
NSLog(@"The values appear in dictionary are:\n %@ \n %@ \n %@",studentNameDictionary[@"st2"],studentNameDictionary[@"st1"],studentNameDictionary[@"st4"],studentNameDictionary[@"st3"]);
}
In main section
Student *student1=[[Student alloc]init];
[student1 getStudentName];
[student1 printStudentName];
The output of above tutorial is shown as:
Source Code Available : Download Here
Also there is one assignment to you. For simplicity i have add values statically you can add values at Console or run time.
I hope you like my tutorial. If you have any query in your mind you can mail me or comment here.


I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in IOS development, kindly contact us http://www.maxmunus.com/contact
ReplyDeleteMaxMunus Offer World Class Virtual Instructor led training on IOS development . We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us:
Name : Arunkumar U
Email : arun@maxmunus.com
Skype id: training_maxmunus
Contact No.-+91-9738507310
Company Website –http://www.maxmunus.com