I didn't finish the thing yesterday, I made up today.
//
Main.m
The fourth day of the four weeks OC Fifth lecture
//
Created by Scjy on 15/11/5.
Copyright (c) The fourth week of 2015. All rights reserved.
//
Fourth of OC, Third day of week four
Dictionary class {Collection class array, dictionary, collection Fast Traversal array sort}
#import <Foundation/Foundation.h>
int main (int argc,constchar * argv[]) {
@autoreleasepool {
Insert code here ...
NSLog (@ "Hello, world!");
Dictionary class
/*
Dictionaries are used to hold data collections with mapping relationships (Key-value)
For name: John, name is key,key corresponding value is John
A key-value combination is considered an entry, and the dictionary is stored key-value to the container
The characteristics of the dictionary class:
Unlike arrays, dictionaries rely on key access elements (arrays are accessed by subscript)
Key cannot be duplicated, value must be object (if not object with Nsnumber,nsvalue)
Key-value pairs are stored unordered in the dictionary
Dictionaries are also divided into variable dictionaries and immutable dictionaries
Immutable dictionary: Once the dictionary is created, the key value cannot be changed, cannot be added, cannot be deleted, can only read key or value
Variable dictionaries: After the dictionary is created, the key values can be changed, can be added, can be deleted, or read key or value
The elements in the dictionary appear in pairs and cannot appear in a single number (key-value pairs with key values are a pair)
Eat, sleep, play keyboard.
*/
Create a dictionary Nsdictionary
Nsdictionary *dic = [[Nsdictionary Alloc]initwithobjectsandkeys:
@ "BenZ", @ "Car1",
@ "Zhang", @ "Car2",
@ "Eat", @ "Car3",
@ "Sleep", @ "CAR4",
@ "beat Peas", @ "CAR5",
@ "Actually is playing keyboard", @ "CAR6", nil];
Get the number of dictionary elements
Nsuinteger count = Dic.count;
NSLog (@ "count =%lu", count);
//
Get all key values
Nsarray *allkeyarray = Dic.allkeys;
//
for (int i = 0; i<allkeyarray.count; i++) {
NSLog (@ "allkeyarray[%d] =%@", I,[allkeyarray objectatindex:i]);
// }
NSLog (@ "%@", [dic AllKeys]);
//
//
Get all value values
Nsarray *allvaluesarray = dic.allvalues;
//
for (int i = 0; i<allvaluesarray.count; i++) {
NSLog (@ "allvaluesarray[%d] =%@", I,[allvaluesarray objectatindex:i]);
// }
NSLog (@ "%@", [dic allvalues]);
//
//
Query value value by key
NSString *car1 = [dic valueforkey:@ "Car1"];
NSLog (@ "car =%@", car1);
//
NSString *car2 = [dic valueforkey:@ "Car2"];
NSLog (@ "car =%@", car2);
//
Variable dictionaries
Initialization method
Nsmutabledictionary *mdictionary = [[Nsmutabledictionary Alloc]initwithobjectsandkeys:
@ "BenZ", @ "Car1",
@ "Zhang", @ "Car2",
@ "Eat", @ "Car3",
@ "Sleep", @ "CAR4",
@ "beat Peas", @ "CAR5",
@ "Actually is playing keyboard", @ "CAR6", nil];
Initializes a new variable dictionary using an existing immutable dictionary
Nsmutabledictionary *mdic1 = [[Nsmutabledictionary alloc]initwithdictionary:dic];
//
To create a new, empty variable dictionary using the Convenience builder method
Nsmutabledictionary *mdic2 = [Nsmutabledictionary dictionary];
//
Delete and change
Add a key value pair
Gaolen is key, tease is value
[Mdictionary setobject:@ "Doubi" forkey:@ "Gaoleng"];
NSLog (@ "%@", mdictionary);
Modify value for key
[Mdictionary setobject:@ "Shifou" forkeyedsubscript:@ "car2"];
NSLog (@ "%@", mdictionary);
//
[Mdictionary setobject:@ "Shifou" forkeyedsubscript:@ "CAR7"];
NSLog (@ "%@", mdictionary);
//
Setvalue:forvalue: When to add a key value pair, when to modify the key value pairs.
When this key is not in the dictionary, Setvalue:forvalue: Just add a new key value pair
When the dictionary has this key, Setvalue:forvalue: is to modify the current key corresponding to the value
//
//
Delete key value pairs
[Mdictionary removeobjectforkey:@ "Car3"];
NSLog (@ "%@", mdictionary);
Traversal key value pairs
Nsarray *array10=[mdictionary AllKeys];
for (int i=0; i<array10.count; i++) {
NSLog (@ "%@%@", [Mdictionary Valueforkey:array10[i]], array10[i]);
// }
Traversal key-value pairs method 2