OC dictionary: How to Use the NSDictionary class, ocnsdictionary
A dictionary is a set of keywords and their definitions (Descriptions. The NSDictionary set that implements dictionaries in Cocoa stores a value (which can be any type of object) under a given keyword (usually an NSString ). Then you can use this keyword to find the corresponding value.
Sharing and adding: txs8882909
1 //
2 // MyClass. h
3 // FoundationKit4
4 //
5 // Created by Elf Sundae on 10/22/10.
6 // Copyright 2010 Control-Strength. All rights reserved.
7 //
8
9 # import <Cocoa/Cocoa. h>
10
11
12 @ interface MyClass: NSObject
13 {
14 NSString * firstName;
15 NSString * lastName;
16}
17
18
19-(void) setFirstName :( NSString *) m_firstName;
20-(NSString *) firstName;
21
22-(void) setLastName: (NSString *) m_lastName;
23-(NSString *) lastName;
24
25
26 @ end
1 //
2 // MyClass. m
3 // FoundationKit4
4 //
5 // Created by Elf Sundae on 10/22/10.
6 // Copyright 2010 Control-Strength. All rights reserved.
7 //
8
9 # import "MyClass. h"
10
11
12 @ implementation MyClass
13
14-(void) setFirstName :( NSString *) m_firstName {
15
16 firstName = m_firstName;
17}
18-(NSString *) firstName {
19 return firstName;
20}
21
22-(void) setLastName: (NSString *) m_lastName {
23 lastName = m_lastName;
24}
25-(NSString *) lastName {
26 return lastName;
27}
28
29
30-(NSString *) description
31 {
32 if (firstName = nil | lastName = nil ){
33 return @ "No Name found .";
34} else {
35 return [NSString stringWithFormat: @ "% @",
36 firstName, lastName];
37}
38
39}
40
41 @ end
1 /*
2 * example Dictionary (NSDictionary, NSMutableDictionary) Operation
3 *
4 * Elf Sundae 10/22/2010
5 */
6
7 # import <Foundation/Foundation. h>
8 # import "MyClass. h"
9
10 int main (int argc, const char * argv []) {
11 NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
12
13 // create a dictionary: dictionaryWithObjectsAndKeys:
14 MyClass * my1 = [MyClass new];
15 MyClass * my2 = [MyClass new];
16 MyClass * my3 = [MyClass new];
17 MyClass * my4 = [MyClass new];
18
19 NSDictionary * myClassDict;
20 myClassDict = [NSDictionary dictionaryWithObjectsAndKeys:
21 my1, @ "my1 ",
22 my2, @ "my2 ",
23 my3, @ "my3 ",
24 my4, @ "my4", nil];
25 // get the value objectForKey
26 MyClass * sub = [myClassDict objectForKey: @ "my3"];
27 if (sub = nil ){
28 exit (1 );
29}
30 [sub setFirstName: @ "Elf"];
31 [sub setLastName: @ "Sundae"];
32
33 NSLog (@ "modify data: % @", sub );
34
35 // traverse the dictionary
36 NSLog (@ "*** traversal dictionary myClassDict :");
37 for (id key in myClassDict)
38 {
39 NSLog (@ "key: % @, value: % @", key, [myClassDict objectForKey: key]);
40}
41 NSLog (@ "*** traversal dictionary myClassDict ends. ");
42
43 // MARK: *** add new element ***
44 // NSDictionary cannot add or delete elements. You can use NSMutableDictionary.
45 NSMutableDictionary * myNewDict = [NSMutableDictionary dictionary];
46 // Add a pair of elements to the new dictionary
47 // [myNewDict setObject: myClassDic forKey: @ "Old immutable dictionary myClassDic"];
48
49 // Add existing data through traversal (original dictionary)
50 for (id key in myClassDict)
51 {
52 [myNewDict setObject: [myClassDict objectForKey: key]
53 forKey: key];
54}
55
56 NSString * newkey = @ "newKey ";
57 NSString * newValue = @ "This is a new Value .";
58 [myNewDict setObject: newValue forKey: newkey];
59
60 // traverse myNewDict
61 NSLog (@ "*** traverse the dictionary myNewDict as follows :");
62 for (id key in myNewDict)
63 {
64 NSLog (@ "key: % @, value: % @", key, [myNewDict objectForKey: key]);
65}
66 NSLog (@ "*** traversal dictionary myNewDict ends. ");
67
68 // delete an element
69 [myNewDict removeObjectForKey: @ "newKey"];
70
71 // traverse myNewDict
72 NSLog (@ "*** traverse the dictionary myNewDict as follows :");
73 for (id key in myNewDict)
74 {
75 NSLog (@ "key: % @, value: % @", key, [myNewDict objectForKey: key]);
76}
77 NSLog (@ "*** traversal dictionary myNewDict ends. ");
78
79 [pool drain];
80 return 0;
81}
// Output result (date and time are omitted)
Modify data: Elf Sundae
* ** The myClassDict dictionary is as follows:
Key: my3, value: Elf Sundae
Key: my4, value: No Name found.
Key: my1, value: No Name found.
Key: my2, value: No Name found.
* ** The myClassDict dictionary is traversed.
* ** The myNewDict dictionary is traversed as follows:
Key: newKey, value: This is a new Value.
Key: my3, value: Elf Sundae
Key: my4, value: No Name found.
Key: my1, value: No Name found.
Key: my2, value: No Name found.
* ** The myNewDict dictionary is traversed.
* ** The myNewDict dictionary is traversed as follows:
Key: my3, value: Elf Sundae
Key: my4, value: No Name found.
Key: my1, value: No Name found.
Key: my2, value: No Name found.
* ** The myNewDict dictionary is traversed.