iPhone : Exploring the Foundation Framework – NS Dictionary Class


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).

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”,  @ “krishna706gmail.com; @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);

0 Responses on iPhone : Exploring the Foundation Framework – NS Dictionary Class"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.