• Home
  • iPhone
  • Exploring the Foundation Framework in iPhone - NS Dictionary Class

Exploring the Foundation Framework in iPhone - NS Dictionary Class

  • (4.0)
  • | 890 Ratings


The NSDictionary class declares the programmatic interface to Objects that manage immutable associations of keys and values. Use this class or its subclass NSMutableDictionary, when you need a convenient and efficient way to retrieve data associated with an arbitrary key. NSDictionary creates static dictionaries, and NSMutableDictionary creates dynamic dictionaries. (To know further class types, refer to Exploring The Foundation Framework).

Inclined to build a profession as IOS Developer? Then here is the blog post on iPhone Development Certification Training.

A key-value pair within a dictionary is called an entry. Each entry consists of one object that represents the key and a second object that is, that key’s value. Within a dictionary, the keys are unique. That is, no two keys in a single dictionary are equal (as determined by isEqual:). Neither a key nor a value can be nil; if you need to represent a null value in a dictionary, you should use NSNull.

Array——- -->   Screenshot_1806 index
Dictionary——- -->  Screenshot_1806 key


    Sign up form:

        First name
         Last name
         Mail id
         Ph no


Kumar   = “first Name”
  NSDictionary   *a Dict  =[[NSDictionary  alloc]
initWithObjectsAndKeys :@  “Kumar”, @ “firstName”,@ “Krishna”, @ “last name”,  @ “; @mailId”, @ “age”,   nil];
NSLog (@ “a Dict is %@”, a Dict);

// Using class method:

NSDictionary *bDict  = [NSDictionary   dictionaryWithObjectsAnd keys  :@  “Kumar”,
@ “first name”, @ “Krishna”, @ “last name”, nil];


Int Count  =   [aDict   Count];

//Getting Object from dict:

NSString *str  = [aDict objectFor key : @ “FirstName];
NSLog (@ “str is % @ and Count is % d”, Str, Count);

// adding Dicts to array:

NSArray  * aArray  =   [NSArray array WithObjects: aDict, bDict, nil];
Ns log (@ “ the array is %@”,  aArray);


The array is
            Age= 25;
First name   =   Kumar;
Last name    = Krishna;
Mail id = “Krishna706@GMAIL.COM”;
First name    =  Kumar ;
Last name    =   Krishna ;

// How to get second dict first name   element in the above array:

NSDictionary *CDict  =  [aArray ObjectAtIndex.1];
NSString *reqElement =  [c Dict object for key:@  “first name”];
NSLog(@ “req Element is %@”, req Element);

//how to add object to an dictionary:

  NSMutableDictionary *a Mutt Dict =
[NSMutableDictionary dictionaryWithDictionary: aDict];
[a mutDict SetObject:@ “100” for key :@ “marks”];
NSLog (@ “a mutDict after adding marks is %@; a mutDict);

// removing an object:

       [a mutDict removeObjectForkey :@  “lastName”];
NSLog (@ “the removing object is %@, a mutDict);

// removing all objects

   [a mutDict removeAllObjects];
NSLog (@ “the removing all objects %@, a mutDict);

//how to get particular element in all dictionaries as array of dictionaries:

NSArray    * firstNamesArray   =
[[aArray valueFor key :@ “firstName”] all objects]:
NSLog (@   “ firstNamesArray is %@”, firstNamesArray);

//how to get keys an array:

NSArray *keysArray =  [aDict all keys];
NSLog  (@  “the key is %@”,  keysArray);

// how to get all objects in a Dict as an array:

NSArray *objectsArray=[aDict  all values];
NSLog (@”the key for objects is %@”, objectsArray);

Frequently Asked iPhone Interview Questions & Answers

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Ravindra Savaram
About The Author

Ravindra Savaram is a Content Lead at His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter. Protection Status