转---NSDictionary类使用

baacloud免费翻墙vpn注册使用

字典:NSDictionary

 

字典就是关键字及其定义(描述)的集合。Cocoa中的实现字典的集合NSDictionary在给定的关键字(通常是一个NSString)下存储一个数值(可以是任何类型的对象)。然后你就可以用这个关键字来查找相应的数值。
不同于数组,字典(也被称为散列表或关联数组)使用的是键查询的优化存储方式。它可以立即找出要查询的数据,而不需要遍历整个数组进行查找。
可使用dictionaryWithObjectsAndKeys来创建字典
查询字典的值:objectForKey
NSMutableDictionary的dictionary方法可以创建一个可变字典,也可以使用dictionaryWithCapaticy:。
使用 setObject:forkey: 方法添加字典元素,如果关键字已存在,则用新植替换旧值。
类似的,NSMutableDictionary类允许随意添加或删除字典元素。
添加元素:setObject:forkey:
删除元素:removeObjectForKey:
Example:

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 * 示例字典(NSDictionary,NSMutableDictionary)操作
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 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
12
13 // 创建字典: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 // 获取值 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(@”修改数据: %@”,sub);
34
35 // 遍历字典
36 NSLog(@”***遍历字典myClassDict如下:”);
37 for (id key in myClassDict)
38 {
39 NSLog(@”key: %@ ,value: %@”,key,[myClassDict objectForKey:key]);
40 }
41 NSLog(@”***遍历字典myClassDict结束。”);
42
43 // MARK: *** 添加新元素 ***
44 // NSDictionary无法添加或删除元素,可以使用NSMutableDictionary.
45 NSMutableDictionary *myNewDict = [NSMutableDictionary dictionary];
46 // 将原有字典添加到新字典的一对元素
47 //[myNewDict setObject:myClassDic forKey:@”旧的不可变字典myClassDic”];
48
49 // 遍历添加已有数据(原字典)
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 // 遍历myNewDict
61 NSLog(@”***遍历字典myNewDict如下:”);
62 for (id key in myNewDict)
63 {
64 NSLog(@”key: %@ ,value: %@”,key,[myNewDict objectForKey:key]);
65 }
66 NSLog(@”***遍历字典myNewDict结束。”);
67
68 // 删除元素
69 [myNewDict removeObjectForKey: @”newKey”];
70
71 // 遍历myNewDict
72 NSLog(@”***遍历字典myNewDict如下:”);
73 for (id key in myNewDict)
74 {
75 NSLog(@”key: %@ ,value: %@”,key,[myNewDict objectForKey:key]);
76 }
77 NSLog(@”***遍历字典myNewDict结束。”);
78
79 [pool drain];
80 return 0;
81 }

 

 

// 输出结果(省略日期 时间等信息)
修改数据: Elf Sundae
***遍历字典myClassDict如下:
key: my3 ,value: Elf Sundae
key: my4 ,value: No Name found.
key: my1 ,value: No Name found.
key: my2 ,value: No Name found.
***遍历字典myClassDict结束。
***遍历字典myNewDict如下:
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.
***遍历字典myNewDict结束。
***遍历字典myNewDict如下:
key: my3 ,value: Elf Sundae
key: my4 ,value: No Name found.
key: my1 ,value: No Name found.
key: my2 ,value: No Name found.
***遍历字典myNewDict结束。

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏