1 /*******************************************************************************************
2 NSArray
3 *******************************************************************************************/
4
5 /*---------------------------建立數組------------------------------*/
6 //NSArray *array = [NSArray alloc] initWithObjects:
7 @"One",@"Two",@"Three",@"Four",nil];
8
9 self.dataArray = array;
10 [array release];
11
12 //- (unsigned) Count;數組所包含對象個數;
13 NSLog(@"self.dataArray cound:%d",[self.dataArray count]);
14
15 //- (id) objectAtIndex: (unsigned int) index;擷取指定索引處的對象;
16 NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);
17
18
19 /*--------------------------從一個數組拷貝資料到另一數組(可變數級)----------------------------*/
20
21 //arrayWithArray:
22 //NSArray *array1 = [NSArray alloc] init];
23 NSMutableArray *MutableArray = [NSMutableArray alloc] init];
24 NSArray *array = [NSArray arrayWithObjects:
25 @"a",@"b",@"c",nil];
26 NSLog(@"array:%@",array);
27 MutableArray = [NSMutableArray arrayWithArray:array];
28 NSLog(@"MutableArray:%@",MutableArray);
29
30 array1 = [NSArray arrayWithArray:array];
31 NSLog(@"array1:%@",array1);
32
33
34 //Copy
35
36 //id obj;
37 NSMutableArray *newArray = [NSMutableArray alloc] init];
38 NSArray *oldArray = [NSArray arrayWithObjects:
39 @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
40
41 NSLog(@"oldArray:%@",oldArray);
42 for(int i = 0; i < [oldArray count]; i++)
43 {
44 obj = [oldArray objectAtIndex:i] copy];
45 [newArray addObject: obj];
46 }
47 //
48 NSLog(@"newArray:%@", newArray);
49 [newArray release];
50
51
52 //快速枚舉
53
54 //NSMutableArray *newArray = [NSMutableArray alloc] init];
55 NSArray *oldArray = [NSArray arrayWithObjects:
56 @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
57 NSLog(@"oldArray:%@",oldArray);
58
59 for(id obj in oldArray)
60 {
61 [newArray addObject: obj];
62 }
63 //
64 NSLog(@"newArray:%@", newArray);
65 [newArray release];
66
67
68 //Deep copy
69
70 //NSMutableArray *newArray = [NSMutableArray alloc] init];
71 NSArray *oldArray = [NSArray arrayWithObjects:
72 @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
73 NSLog(@"oldArray:%@",oldArray);
74 newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);
75 NSLog(@"newArray:%@", newArray);
76 [newArray release];
77
78
79 //Copy and sort
80
81 //NSMutableArray *newArray = [NSMutableArray alloc] init];
82 NSArray *oldArray = [NSArray arrayWithObjects:
83 @"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];
84 NSLog(@"oldArray:%@",oldArray);
85 NSEnumerator *enumerator;
86 enumerator = [oldArray objectEnumerator];
87 id obj;
88 while(obj = [enumerator nextObject])
89 {
90 [newArray addObject: obj];
91 }
92 [newArray sortUsingSelector:@selector(compare:)];
93 NSLog(@"newArray:%@", newArray);
94 [newArray release];
95
96
97
98 /*---------------------------切分數組------------------------------*/
99
100 //從字串分割到數組- componentsSeparatedByString:
101 NSString *string = [NSString alloc] initWithString:@"One,Two,Three,Four"];
102 NSLog(@"string:%@",string);
103 NSArray *array = [string componentsSeparatedByString:@","];
104 NSLog(@"array:%@",array);
105 [string release];
106
107
108 //從數組合并元素到字串- componentsJoinedByString:
109 NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
110 NSString *string = [array componentsJoinedByString:@","];
111 NSLog(@"string:%@",string);
112
113
114
115 /*******************************************************************************************
116 NSMutableArray
117 *******************************************************************************************/
118 /*---------------給數組分配容量----------------*/
119 //NSArray *array;
120 array = [NSMutableArray arrayWithCapacity:20];
121
122
123
124 /*--------------在數組末尾添加對象----------------*/
125 //- (void) addObject: (id) anObject;
126 //NSMutableArray *array = [NSMutableArray arrayWithObjects:
127 @"One",@"Two",@"Three",nil];
128 [array addObject:@"Four"];
129 NSLog(@"array:%@",array);
130
131
132
133 /*--------------刪除數組中指定索引處對象----------------*/
134 //-(void) removeObjectAtIndex: (unsigned) index;
135 //NSMutableArray *array = [NSMutableArray arrayWithObjects:
136 @"One",@"Two",@"Three",nil];
137 [array removeObjectAtIndex:1];
138 NSLog(@"array:%@",array);
139
140
141
142 /*-------------數組枚舉---------------*/
143 //- (NSEnumerator *)objectEnumerator;從前向後
144 //NSMutableArray *array = [NSMutableArray arrayWithObjects:
145 @"One",@"Two",@"Three",nil];
146 NSEnumerator *enumerator;
147 enumerator = [array objectEnumerator];
148
149 id thingie;
150 while (thingie = [enumerator nextObject]) {
151 NSLog(@"thingie:%@",thingie);
152 }
153
154
155 //- (NSEnumerator *)reverseObjectEnumerator;從後向前
156 //NSMutableArray *array = [NSMutableArray arrayWithObjects:
157 @"One",@"Two",@"Three",nil];
158 NSEnumerator *enumerator;
159 enumerator = [array reverseObjectEnumerator];
160
161 id object;
162 while (object = [enumerator nextObject]) {
163 NSLog(@"object:%@",object);
164 }
165
166
167 //快速枚舉
168 //NSMutableArray *array = [NSMutableArray arrayWithObjects:
169 @"One",@"Two",@"Three",nil];
170 for(NSString *string in array)
171 {
172 NSLog(@"string:%@",string);
173 }
174
175
176
177 /*******************************************************************************************
178 NSDictionary
179 *******************************************************************************************/
180
181 /*------------------------------------建立字典------------------------------------*/
182 //- (id) initWithObjectsAndKeys;
183
184 //NSDictionary *dictionary = [NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];
185 NSString *string = [dictionary objectForKey:@"One"];
186 NSLog(@"string:%@",string);
187 NSLog(@"dictionary:%@",dictionary);
188 [dictionary release];
189
190
191 /*******************************************************************************************
192 NSMutableDictionary
193 *******************************************************************************************/
194
195 /*------------------------------------建立可變字典------------------------------------*/
196 //建立
197 NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
198
199 //添加字典
200 [dictionary setObject:@"One" forKey:@"1"];
201 [dictionary setObject:@"Two" forKey:@"2"];
202 [dictionary setObject:@"Three" forKey:@"3"];
203 [dictionary setObject:@"Four" forKey:@"4"];
204 NSLog(@"dictionary:%@",dictionary);
205
206 //刪除指定的字典
207 [dictionary removeObjectForKey:@"3"];
208 NSLog(@"dictionary:%@",dictionary);
209
210
211 /*******************************************************************************************
212 NSValue(對任何對象進行封裝)
213 *******************************************************************************************/
214
215 /*--------------------------------將NSRect放入NSArray中------------------------------------*/
216 //將NSRect放入NSArray中
217 NSMutableArray *array = [NSMutableArray alloc] init];
218 NSValue *value;
219 CGRect rect = CGRectMake(0, 0, 320, 480);
220 value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];
221 [array addObject:value];
222 NSLog(@"array:%@",array);
223
224 //從Array中提取
225 value = [array objectAtIndex:0];
226 [value getValue:&rect];
227 NSLog(@"value:%@",value);
228
229
230 /*******************************************************************************************
231 從目錄搜尋副檔名為jpg的檔案
232 *******************************************************************************************/
233
234 //NSFileManager *fileManager = [NSFileManager defaultManager];
235 NSString *home;
236 home = @"../Users/";
237
238 NSDirectoryEnumerator *direnum;
239 direnum = [fileManager enumeratorAtPath: home];
240
241 NSMutableArray *files = [NSMutableArray alloc] init];
242
243 //枚舉
244 NSString *filename;
245 while (filename = [direnum nextObject]) {
246 if([filename pathExtension] hasSuffix:@"jpg"]){
247 [files addObject:filename];
248 }
249 }
250
251 //快速枚舉
252 //for(NSString *filename in direnum)
253 //{
254 // if([filename pathExtension] isEqualToString:@"jpg"]){
255 // [files addObject:filename];
256 // }
257 //}
258 NSLog(@"files:%@",files);
259
260 //枚舉
261 NSEnumerator *filenum;
262 filenum = [files objectEnumerator];
263 while (filename = [filenum nextObject]) {
264 NSLog(@"filename:%@",filename);
265 }
266
267 //快速枚舉
268 //for(id object in files)
269 //{
270 // NSLog(@"object:%@",object);
271 //}
摘自 技術成就理想