1
#pragma mark *****************************字典********************************
2
//
字典:通过key来取值的 key值必须是成对出现的,key不能为空(nil)
3 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
@"
object
",
@"
key
",
@"
object2
",
@"
key2
", nil];
4 NSDictionary *dic1 = @{
@"
key1
":
@"
object1
",
@"
key2
":
@"
object2
",
@"
key3
":
@"
object3
",
@"
key4
":
@"
object4
"};
5 NSLog(
@"
%@
",[dic objectForKey:
@"
key
"]);
6 NSLog(
@"
%@
",dic1[
@"
key2
"]);
7
//
dic.allKeys可以取出dic中所有的key值;
8 NSLog(
@"
%@
",dic1.allKeys);
9
for(NSString *key
in dic1.allKeys){
10 NSLog(
@"
%@
",[dic1 objectForKey: key]);
11 }
12
#pragma mark *****************************可变字典********************************
13
//
可变字典:可以删除、替换(添加)
14
//
NSMutableDictionary 可变字典的类名
15 NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
//
初始化了一个空字典
16 [dictionary setObject:
@"
iphone
" forKey:
@"
phone
"];
17
18 [dictionary setObject:
@"
htc
" forKey:
@"
phone
"];
19 NSLog(
@"
%@
",dictionary);
20 [dictionary removeObjectForKey:
@"
phone
"];
21 NSLog(
@"
%@
",dictionary);
22
//
不可以这种方式初始化NSMutableDictionary *dictionary =@{@"key1":@"object1",@"key2":@"object2"};
23
#pragma mark **************************字典之间的嵌套******************************
24
//
不可变数组
25 NSDictionary *date = @{
@"
id
":
@"
110119
",
@"
nick
":
@"
小花
",
@"
header
":
@"
www.baidu.headeerImage.png
",
@"
balance
":
@"
100
",};
26 NSDictionary *dictionary1 = @{
@"
code
":
@"
200
",
@"
data
":date};
27 NSLog(
@"
%@
",dictionary1);
28
//
可变数组
29 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
30 [dict setObject:@(
200) forKey:
@"
code
"];
31 [dict setObject:date forKey:
@"
data
"];
32
33 NSLog(
@"
%@
",dict);
34 NSMutableDictionary *dict1 = [dict[
@"
data
"] mutableCopy];
35 [dict1 setObject:
@"
www.baidu.taobao.Image.png
" forKey:
@"
header
"];
36 NSLog(
@"
%@
",dict1);
37
//
输出呢称
38 NSLog(
@"
呢称是:%@
",dictionary1[
@"
data
"][
@"
nick
"]);
39
40
//
判断code值是不是200,如果code的返回值是200 输出data字典里面的所有内容,如果不是200输出返回数据失败
41
switch ([dict[
@"
code
"] integerValue]) {
42
case
200:{
43 NSLog(
@"
登陆成功
");
44 NSDictionary *userInfo = dict[
@"
data
"];
45 NSLog(
@"
呢称是%@
",userInfo[
@"
nick
"]);
46 }
47
break;
48
case
300:
49 NSLog(
@"
返回数据失败
");
50
break;
51
case
310:
52 NSLog(
@"
用户失效
");
53
break;
54
default:
55 NSLog(
@"
未知错误
");
56
break;
57 }
58
//
59
60
61
62 NSMutableDictionary *liuDic0 = [NSMutableDictionary dictionary];
63 [liuDic0 setObject:@(
200) forKey:
@"
code
"];
64 NSDictionary *liuData0 = @{
@"
id
":
@"
LiYongJun250
",
@"
name
":
@"
LiYongJun250
",
@"
phone
":
@"
15761672937
",
@"
balance
":
@"
1000.10
",
@"
msgnum
":
@"
20
"};
65 [liuDic0 setValue:liuData0 forKey:
@"
data
"];
66
67 NSMutableDictionary *liuDic1 = [NSMutableDictionary dictionary];
68 [liuDic1 setObject:@(
200) forKey:
@"
code
"];
69
70 NSDictionary *liuData1 = @{
@"
version
":
@"
1.0.1
",
@"
desc
":
@"
LiYongJun250
",
@"
url
":
@"
www.baidu.LiYongJun250.com
"};
71 [liuDic1 setObject:liuData1 forKey:
@"
data
"];
72 NSInteger phoneNum = [liuDic0[
@"
data
"][
@"
phone
"] integerValue];
73 NSLog(
@"
手机号是:%ld
",phoneNum);
74 NSLog(
@"
下载地址是:%@
",liuDic1[
@"
data
"][
@"
url
"]);
75
76
77
#pragma mark **************************字典与数组之间的嵌套******************************
78
79 NSArray *arr = @[liuData0,@(
111),@(
2222),@(
33333)];
80 NSLog(
@"
%@
",arr);
81 NSDictionary *dicti = arr[
0];
82 NSLog(
@"
%@
",dicti);
83
执行结果:
2015-06-24 19:45:28.258 OC-NO-4-可变数组[806:83680] object
2015-06-24 19:45:28.259 OC-NO-4-可变数组[806:83680] object2
2015-06-24 19:45:28.260 OC-NO-4-可变数组[806:83680] (
key3,
key1,
key4,
key2
)
2015-06-24 19:45:28.260 OC-NO-4-可变数组[806:83680] object3
2015-06-24 19:45:28.260 OC-NO-4-可变数组[806:83680] object1
2015-06-24 19:45:28.260 OC-NO-4-可变数组[806:83680] object4
2015-06-24 19:45:28.260 OC-NO-4-可变数组[806:83680] object2
2015-06-24 19:45:28.260 OC-NO-4-可变数组[806:83680] {
phone = htc;
}
2015-06-24 19:45:28.260 OC-NO-4-可变数组[806:83680] {
}
2015-06-24 19:45:28.261 OC-NO-4-可变数组[806:83680] {
code = 200;
data = {
balance = 100;
header = "www.baidu.headeerImage.png";
id = 110119;
nick = "\U5c0f\U82b1";
};
}
2015-06-24 19:45:28.261 OC-NO-4-可变数组[806:83680] {
code = 200;
data = {
balance = 100;
header = "www.baidu.headeerImage.png";
id = 110119;
nick = "\U5c0f\U82b1";
};
}
2015-06-24 19:45:28.261 OC-NO-4-可变数组[806:83680] {
balance = 100;
header = "www.baidu.taobao.Image.png";
id = 110119;
nick = "\U5c0f\U82b1";
}
2015-06-24 19:45:28.299 OC-NO-4-可变数组[806:83680] 呢称是:小花
2015-06-24 19:45:28.299 OC-NO-4-可变数组[806:83680] 登陆成功
2015-06-24 19:45:28.300 OC-NO-4-可变数组[806:83680] 呢称是小花
2015-06-24 19:45:28.300 OC-NO-4-可变数组[806:83680] 手机号是:15761672937
2015-06-24 19:45:28.300 OC-NO-4-可变数组[806:83680] 下载地址是:www.baidu.LiYongJun250.com
2015-06-24 19:45:28.301 OC-NO-4-可变数组[806:83680] (
{
balance = "1000.10";
id = LiYongJun250;
msgnum = 20;
name = LiYongJun250;
phone = 15761672937;
},
111,
2222,
33333
)
2015-06-24 19:45:28.301 OC-NO-4-可变数组[806:83680] {
balance = "1000.10";
id = LiYongJun250;
msgnum = 20;
name = LiYongJun250;
phone = 15761672937;
}
转载于:https://www.cnblogs.com/fshmjl/p/4581940.html