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

Exploring the Foundation Framework in iPhone - NS Dictionary Class

 

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

 

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
IOS Development TrainingNov 19 to Dec 04View Details
IOS Development TrainingNov 23 to Dec 08View Details
IOS Development TrainingNov 26 to Dec 11View Details
IOS Development TrainingNov 30 to Dec 15View 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 less