1 //
2 // main.m
3 // 集合類
4 //
5 // Created by sixleaves on 15/5/14.
6 // Copyright (c) 2015年 小碼哥. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11
12 void createSet();
13 void createArray();
14 void createDictionary();
15 int main(int argc, const char * argv[]) {
16
17 createSet();
18 createArray();
19 createDictionary();
20
21 return 0;
22 }
23
24
25 void createArray() {
26 // C語言數組與NSArray的區別
27 /*
28 1.NSArray可以存放任何對象,而C語言只能存放一種類型。
29 2.NSArray不能存放基本數據類型,而C語言數組可以。
30 3.NSArray不能存放空值(nil).
31 */
32
33 // 不可變數組,創建后不可再改變,所以array永遠是空數組
34 // 所以不可變數組,只能在創建的時候放數據進去。
35 NSArray *array = [NSArray array];
36
37 // 0.nil用來標識NSArray中結束的標識
38 // 0.古老方式-創建數組
39 NSArray *array2 = [NSArray arrayWithObjects:@"jack", @"swp", nil];
40
41 // 0.常用方式-創建數組(編譯器特性)
42 NSArray *array3 = @[@"swp", @"gh"];
43
44 // 1.數組元素的個數
45 long len = array2.count; // 點語法
46 NSLog(@"%ld", len);
47
48 // 2.訪問數組
49 // 古老的方式
50 NSLog(@"%@", [array2 objectAtIndex: 1]); // 從0開始計數
51
52 // 常用方式(編譯器特性)
53 NSLog(@"%@", array2[1]);
54
55 // 3.數組遍歷方式
56 // 3.1-古老方式
57 NSArray * array5 = @[@"fuck", @"what", @"you"];
58 for ( int i = 0; i < array5.count; i++) {
59 NSLog(@"%@", array5[i]); // NSArray中的元素都是對象
60 // 而對象打印出都是用%@
61 }
62
63 // 3.1-常用方式
64 for (id obj in array5) {
65 NSLog(@"%@", obj);
66 }
67
68 // 3.1-常用方式-block
69 [array5 enumerateObjectsUsingBlock:
70 ^(id obj, NSUInteger idx, BOOL *stop) {
71 NSLog(@"%ld-%@",idx, obj);
72 *stop = YES;
73 }
74 ]; // 每拿到一個元素都會回調block,obj就是元素,idx為元素的對應索引
75 // stop是用來表示是否繼續下一次循環。如果設置成YES本次循環結束
76 // 就不會繼續下一次。
77 /*
78 NSArray總結:
79 1.創建方式(常用-古老)
80
81 2.訪問方式(常用-古老)
82
83 3.遍歷方式(三種)
84 3.1 直接for循環
85 3.2 for-in循環
86 3.3 使用block
87 */
88
89
90 // 1-1.創建. 創建不可變數組只能通過方法創建。(@[]創建的不可變數組)
91 NSMutableArray *array7 = [NSMutableArray array]; // arrayWithObjects
92
93 // 2-1 添加
94 [array7 addObject:@"swp"];
95
96 // 3-1 刪除
97 [array7 removeObject:@"swp"];
98 // [array removeObjectAtIndex:0];
99 // [array removeAllObject];
100
101 // 遍歷
102 for (id obj in array7) {
103
104 NSLog(@"%@", obj);
105 }
106
107
108 }
109
110 void createSet() {
111 /*
112 NSSet與NSArray最大的區別就是NSSet是無序的。
113 */
114 NSSet *s = [NSSet setWithObjects:@"swp", @"fuck", nil];
115
116 NSLog(@"len = %ld", s.count);
117
118 /*
119 因為set是無序的,所以從set中取東西邏輯上就不可能有序,也就是隨機的。
120 */
121 NSString *str = [s anyObject];
122
123 NSLog(@"str = %@", s);
124
125 // 創建
126 NSMutableSet *s2 = [NSMutableSet set];
127
128 // 添加
129 [s2 addObject:@"fuck"];
130
131 // 遍歷
132 for (id obj in s2) {
133 NSLog(@"obj in set is = %@", obj);
134 }
135
136 // NSSet不能使用block進行遍歷
137
138 // 刪除
139 [s2 removeObject:@"fuck"];
140
141
142
143 /*
144 NSSet與NSArray的對比
145 1》共同點
146 * 都是集合、能存放多個OC對象。
147 * 都不能存放基本數據類型、結構體、枚舉、共用體
148 * 本身都是不可變的、都有一個可變的子類。
149
150 2》不同點
151 1.NSSet是無序的,NSArray是有序的。
152
153 */
154 }
155
156 void createDictionary() {
157 /*
158 NSDictionary: 表示的是一種映射關系,是不可變對象。
159 NSMutableDictionary: 可變對象。
160 字典也是沒有順序的。
161 */
162 // 1-1.創建的是空字典、
163 NSDictionary * dict1 =[NSDictionary dictionary];
164
165 // 1-2.根據值數組和鍵數組創建字典。
166 NSArray *keys = @[@"name", @"address"];
167 NSArray *values = @[@"jack", @"北京"];
168
169 NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];
170 //NSLog(@"dict2 = %@", dict2);
171
172 // 1-3.直接根據鍵值對創建
173 NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:
174 @"jack", @"name",
175 @"北京", @"address",
176 @"10086", @"qq", nil];
177 // 1-3.常用方式-編譯器特性.
178 NSDictionary *dict4 = @{@"name": @"jack", @"address" : @"北京"};
179
180 // 2-1.舊方式-訪問字典
181 id obj = [dict4 objectForKey:@"name"];
182
183 // 2-1.新方式-訪問字典
184 id obj1 = dict4[@"name"];
185 // NSLog(@"%@, %@", obj, obj1);
186
187
188 // 3-1.字典鍵值對的大小
189 NSUInteger i = dict4.count;
190 NSLog(@"i = %ld", i);
191
192
193
194 // NSMutableDictionary
195
196 NSMutableDictionary * dict5 = [NSMutableDictionary dictionary];
197
198 // 1.添加
199 [dict5 setObject:@"jack" forKey:@"name"];
200
201 [dict5 setObject:@"rose" forKey:@"name"]; // 會覆蓋掉原來的值
202
203 // 2.刪除(鍵值對)
204
205 // [dict5 removeObjectForKey: @"name"];
206
207 // 3.打印字典, 直接NSLog就行。
208
209 // 4.注意點
210 /*
211 @{}創建的不可變字典,不能將其賦值給可變字典指針!否則可能在程序
212 運行過程中會引起程序崩潰!因為調用了可變字典的方法。
213 */
214
215 }
216
總結:以后一看到Mutable就知道這兩個肯定是父子關系。
NSArray\NSMutableArray:
*有序
*快速創建方式:(不可變) @[]
*快速訪問方式: 數組名[i]
NSSet\NSMutableSet:
*無序
*無快速創建方、和快速訪問方式
NSDictionary\NSMutableDictionary:
*無序
*快速創建方式(不可變): @{}
*快速訪問方式: 字典名[鍵值]