Knowledge points
1.NSDictionary
2.NSMutableDictionary
===========================
Nsdictionary
1. Think: Now you need to find the specified phone number by name
1) Whether the Nsarray can be achieved by
2) is it easy to implement
2.NSDictionary
1) How to create a Dictionary object
Dictionarywithobjectsandkeys message
Action: Creates a Nsdictionary object based on the specified object and keys
2) How to save content
Save by Key-value Way
3) What is a key value, what is a key, what is a value
4) How the dictionary is stored
A dictionary is a collection of unordered saves
3. How to get the corresponding value by key
1) Objectforkey Message
Function: To find the corresponding value by specifying key
4. How to traverse the contents of a dictionary
1) keyenumerator Message
Function: Gets all the key values
2) AllKeys Message
Function: Gets all the key values
3) allvalues Message
Function: Get all values
Comparison of 5.NSDictionary and Nsarray
1) Advantages of dictionary relative to array
Dictionaries get elements faster than arrays, because dictionaries get the exact location of each element through a hashing algorithm
2) How to get elements
Array gets the contents of the specified element by subscript Objectatindex
The dictionary obtains the content of the specified element through key Objectforkey
6. Traversing a dictionary
1) Normal traversal
2) Fast traversal
============================
Nsmutabledictionary
The difference between 1.NSMutableDictionary and nsdictionary
2. How to create
1) Dictionary Message
Role: Create an empty dictionary
2) Dictionarywithobjects:forkeys Message
Function: Creates a Dictionary object by specifying an array
3. Adding a mutable dictionary
1) Setobject:forkey Message
Role: Add a new set of key-value mappings
4. Deleting a mutable dictionary
1) Removeobjectforkey Message
Function: Deletes a set of mappings by specifying key
2) Removeobjectforkeys Message
Function: Deletes a related mapping relationship by specifying an array of contents
3) removeallobjects Message
Role: Delete all mappings in the dictionary
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
@autoreleasepool {
Find-Reference Nsdictional
Nsarray*[email protected][@ "Key1", @ "value1", @ "Key2", @ "calue2"];
Nsstring*[email protected] "Key1";
Nsstring*string1=array[[array indexofobject:string]+1];
Nsstring*string=array[[array indexofobject:@ "Key1"]+1];
NSLog (@ "%@", string);
Creating a #pragma make dictionary
#pragma make a dictionary by using the + method
Create a single creation by using the + method
Nsdictionary*dic1=[nsdictionary dictionarywithobject:@ "value1" forkey:@ "Key1"];
NSLog (@ "dic1:%@", Dic1);
Get value based on key
NSLog (@ "key1:%@", [Dic1 objectforkey:@ "Key1"]);
NSLog (@ "key1:%@", [Dic1 objectforkeyedsubscript:@ "Key1"]);
Create multiple creation with the + method
Nsarray*[email protected][@ "Key1", @ "Key2"];
Nsarray*[email protected][@ "value1", @ "value2"];
Nsdictionary*dic2=[nsdictionary Dictionarywithobjects:objs Forkeys:keys];
NSLog (@ "dic2:%@", Dic2);
NSLog (@ "key1:%@", [Dic2 objectforkey:@ "Key1"]);
NSLog (@ "key2:%@", [Dic2 objectforkeyedsubscript:@ "Key2"]);
#pragma make create a dictionary from an instance
Nsdictionary*dic3=[[nsdictionary alloc]initwithobjectsandkeys:@ "value1", @ "Key1",
@ "value2", @ "Key2",
NIL];
NSLog (@ "dic3:%@", DIC3);
NSLog (@ "key1:%@", [dic3 objectforkey:@ "Key1"]);
NSLog (@ "key2:%@", [dic3 objectforkeyedsubscript:@ "Key2"]);
#pragma make to create a dictionary from an existing dictionary
Nsdictionary*dic4=[nsdictionary DICTIONARYWITHDICTIONARY:DIC3];
NSLog (@ "dic4:%@", DIC4);
NSLog (@ "key1:%@", [Dic4 objectforkey:@ "Key1"]);
NSLog (@ "key2:%@", [Dic4 objectforkeyedsubscript:@ "Key2"]);
#pragma make dictionary literal syntax @{key:value,key1:value}
Nsdictionary*[email protected]{@ "Key1": @ "value1", @ "Key2": @ "value2"};
NSLog (@ "%@", DIC5);
NSLog (@ "key1:%@", dic5[@ "Key1"]);
NSLog (@ "key2:%@", dic5[@ "Key2"]);
#pragma make gets the number of dictionaries
Nsuinteger Leng=dic5.count;
NSLog (@ "Dic5.count:%ld", Leng);
#pragma a collection of make key values
All the key sets
Nsarray*keys1=[dic5 AllKeys];
NSLog (@ "key set:%@", keys1);
All Values Collection
Nsarray*value1=[dic5 Allvalues];
NSLog (@ "%@", value1);
#pragma make variable dictionary
Nsmutabledictionary*mutabledic=[[nsmutabledictionary alloc]initwithobjectsandkeys:@ "mvalue1", @ "Mkey1",
@ "Mvalue2", @ "Mkey2", nil];
#pragma make add delete insert
#pragma make to add a dictionary of an existing dictionary
[Mutabledic ADDENTRIESFROMDICTIONARY:DIC5];
NSLog (@ "%@", mutabledic);
#pragma make to add a new key-value object SetValue
[Mutabledic setvalue:@ "MValue3" forkey:@ "MKey3"];
NSLog (@ "%@", mutabledic);
#pragma make overwrites the old dictionary data with the new dictionary data setdictionary
[Mutabledic SETDICTIONARY:DIC5];
NSLog (@ "%@", mutabledic);
#pragma make to delete value from key
[Mutabledic removeobjectforkey:@ "Key1"];
NSLog (@ "%@", mutabledic);
#pragma make to delete all elements
[Mutabledic removeallobjects];
NSLog (@ "%@", mutabledic);
#pragma make traversal
Nsmutabledictionary*mutabledic1=[[nsmutabledictionary alloc]initwithobjectsandkeys:@ "mValue1", @ "MKey1", @ " MValue2 ", @" MKey2 ", nil];
#pragma make quick traverse
For (ID key1 in MutableDic1) {
NSLog (@ "key:%@ value:%@", Key1, [MutableDic1 objectforkey:key1]);
}
#pragma make enumeration variable [variable array keyenumerator]
ID Key2=[[mutabledic1 Keyenumerator]nextobject];
Nsenumerator*enumerator=[mutabledic1 Keyenumerator];
ID Key2=[enumerator Nextobject];
while (Key2) {
NSLog (@ "%@:%@", Key2,[mutabledic1 Objectforkey:key2]);
Key2=[enumerator Nextobject];
}
}
return 0;
}
/*
#import <Foundation/Foundation.h>
#import "Person.h"
int main (int argc, const char * argv[]) {
@autoreleasepool {
#if 0
Nsarray *array = @[@ "China Telecom" @ "10000" @ "China Mobile" @ "10086"];
NSString *name = @ "China Telecom";
NSString *phone = Array[[array indexofobject:name]+1];
NSLog (@ "%@", phone);
#endif
Person *P1 = [[Person alloc] initwithname:@ "China Telecom" andphone:@ "10000"];
Person *P2 = [[Person alloc] initwithname:@ "China Mobile" andphone:@ "10086"];
Nsarray *array = @[p1, p2];
NSString *name = @ "China Telecom";
For (person *p in array)
{
if ([[P getName] isequaltostring:name])
{
NSLog (@ "%@", [P getphone]);
Break
}
}
#endif
Dictionary Key-value (key-value pairs)
The key value exists only in the dictionary
Dictionary is an unordered collection of hash tables hash table
Instance method creation
Nsdictionary *dic = [[Nsdictionary alloc] initwithobjectsandkeys:@ "10000" @ "China Telecom" @ "10086" @ "China Mobile", nil];
Nsarray *OBJS = @[@ "10000", @ "10086"];
Nsarray *keys = @[@ "China Telecom" @ "China Mobile"];
Create by class method
Nsdictionary *dic = [nsdictionary dictionarywithobjects:objs Forkeys:keys];
NSLog (@ "%@", DIC);
NSString *name = @ "China Telecom";
Gets the value of the dictionary
NSString *phone = [dic objectforkey:name];
NSLog (@ "%@", phone);
The literal syntax of a dictionary
@{key:value, Key:value};
Nsdictionary *dic2 = @{@ "China Telecom": @ "10000" @ "China Mobile": @ "10086"};
Gets the literal syntax of the dictionary value
NSLog (@ "%@", dic2[@ "China Mobile"]);
The traversal of the dictionary!!
Enumerator method
Nsenumerator *enumer = [Dic2 keyenumerator];
ID obj = nil;
while (obj = [Enumer nextobject])
{
NSLog (@ "%@", Dic2[obj]);
}
NSLog (@ "=========== wretched dividing line ============");
Fast enumeration of dictionaries, and the resulting key value
for (id obj in dic2)
{
NSLog (@ "%@", Dic2[obj]);
}
NSLog (@ "=========== wretched dividing line ============");
An array of directly obtained values
Nsarray *values = [Dic2 allvalues];
NSLog (@ "values:%@", values);
Get all the key values directly
Nsarray *keys2 = [Dic2 AllKeys];
NSLog (@ "keys:%@", keys2);
Get key by value
Nsarray *KEYS3 = [Dic2 allkeysforobject:@ "10000"];
NSLog (@ "keysforobj:%@", KEYS3);
/*
The difference between an array and a dictionary
1. Array-ordered collection
Dictionary unordered Collection
2. Get the element mode:
Array by subscript
Dictionary via key
3. Search efficiency
The efficiency of the dictionary is higher,
*/
/*
Variable dictionary
Nsmutabledictionary *mdic = [[Nsmutabledictionary alloc] init];
-description
adding elements
[MDic setobject:@ "10010" forkey:@ "China Unicom"];
NSLog (@ "MDic:%@", mDic);
[MDic Addentriesfromdictionary:dic2];
NSLog (@ "MDic:%@", mDic);
Will overwrite the previous content, key has only one
[MDic setobject:@ "xxxxx" forkey:@ "China Mobile"];
NSLog (@ "MDic:%@", mDic);
Delete Dictionary
[MDic removeobjectforkey:@ "China Unicom"];
NSLog (@ "MDic:%@", mDic);
Delete multiple key values at the same time
[MDic removeobjectsforkeys:@[@ "China Mobile", @ "ddddd"];
NSLog (@ "MDic:%@", mDic);
NSString *str = [mDic objectforkey:@ "222"];
NSLog (@ "%@", str); Nil
Delete all content
[MDic removeallobjects];
NSLog (@ "MDic:%@", mDic);
}
return 0;
}
Person.h
#import <Foundation/Foundation.h>
@interface Person:nsobject
{
NSString *_name;
NSString *_phone;
}
-(Instancetype) Initwithname: (NSString *) name Andphone: (NSString *) phone;
-(NSString *) getName;
-(NSString *) Getphone;
Person.m
#import "Person.h"
@implementation person
-(Instancetype) Initwithname: (NSString *) name Andphone: (NSString *) phone
{
if (self = [super init])
{
_name = name;
_phone = phone;
}
return self;
}
-(NSString *) getName
{
return _name;
}
-(NSString *) Getphone
{
return _phone;
}
@end
@end
*/
4.ios-nsdictional