Home  >  Blog  >   iPhone

Exploring the Foundation Framework in iPhone - NS Dictionary Class

Rating: 4
  
 
2060

 

NS DICTIONARY

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.

MindMajix Youtube Channel

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

Ex:-

    Sign up form:

        First name
         Last name
         Mail id
         Ph no
         Address
         Age
          Marks

Ex:-

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];

//Count

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

O/p:

/*
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 the object to a 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 an array of dictionaries:

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

//how to get keys to 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

 

Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
IOS Development TrainingApr 27 to May 12View Details
IOS Development TrainingApr 30 to May 15View Details
IOS Development TrainingMay 04 to May 19View Details
IOS Development TrainingMay 07 to May 22View Details
Last updated: 03 Apr 2023
About Author

Ravindra Savaram is a Technical Lead at Mindmajix.com. 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.

read more
Recommended Courses

1 / 15