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
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];
Int Count = [aDict Count];
NSString *str = [aDict objectFor key : @ “FirstName];
NSLog (@ “str is % @ and Count is % d”, Str, Count);
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 ;
}
}
*/
NSDictionary *CDict = [aArray ObjectAtIndex.1];
NSString *reqElement = [c Dict object for key:@ “first name”];
NSLog(@ “req Element is %@”, req Element);
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);
NSArray * firstNamesArray =
[[aArray valueFor key :@ “firstName”] all objects]:
NSLog (@ “ firstNamesArray is %@”, firstNamesArray);
NSArray *keysArray = [aDict all keys];
NSLog (@ “the key is %@”, keysArray);
NSArray *objectsArray=[aDict all values];
NSLog (@”the key for objects is %@”, objectsArray);
Frequently Asked iPhone Interview Questions & Answers
Name | Dates | |
---|---|---|
IOS Development Training | Sep 14 to Sep 29 | View Details |
IOS Development Training | Sep 17 to Oct 02 | View Details |
IOS Development Training | Sep 21 to Oct 06 | View Details |
IOS Development Training | Sep 24 to Oct 09 | View Details |
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.