1 /************************************** **************************************** *************
2 NSArray
3 *************************************** **************************************** ************/
4
5/* --------------------------- create an array ------------------------------*/
6 // NSArray * array = [NSArray alloc] initWithObjects:
7 @ "One", @ "Two", @ "Three", @ "Four", nil];
8
9 self. dataArray = array;
10 [array release];
11
12 //-(unsigned) Count; number of objects contained in the array;
13 NSLog (@ "self. dataArray cound: % d", [self. dataArray count]);
14
15 //-(id) objectAtIndex: (unsigned int) index; get the object at the specified index;
16 NSLog (@ "self. dataArray cound 2: % @", [self. dataArray objectAtIndex: 2]);
17
18
19/* ------------------------ copy data from one array to another array (variable level )----------------------------*/
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 // quick Enumeration
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/* --------------------------- split the array ------------------------------*/
99
100 // split the string into an array-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 // merge elements from the array to the string-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/* --------------- allocate capacity to the array ----------------*/
119 // NSArray * array;
120 array = [NSMutableArray arrayWithCapacity: 20];
121
122
123
124/* -------------- add an object at the end of the array ----------------*/
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/* -------------- Delete the object at the specified index in the array ----------------*/
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/* ------------- array enumeration ---------------*/
143 //-(NSEnumerator *) objectEnumerator; forward and backward
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; forward from the back
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 // quick Enumeration
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/* ------------------------------------ create a dictionary ------------------------------------*/
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/* ------------------------------------ create a variable dictionary ------------------------------------*/
196 // create
197 NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
198
199 // Add a dictionary
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 // Delete the specified dictionary
207 [dictionary removeObjectForKey: @ "3"];
208 NSLog (@ "dictionary: % @", dictionary );
209
210
211 /************************************** **************************************** *************
212 NSValue (packaging any object)
213 *************************************** **************************************** ************/
214
215/* -------------------------------- add NSRect to NSArray ------------------------------------*/
216 // put NSRect into 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 // extract from Array
225 value = [array objectAtIndex: 0];
226 [value getValue: & rect];
227 NSLog (@ "value: % @", value );
228
229
230 /************************************** **************************************** *************
231 search for files with the jpg extension from the directory
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 // Enumeration
244 NSString * filename;
245 while (filename = [direnum nextObject]) {
246 if ([filename pathExtension] hasSuffix: @ "jpg"]) {
247 [files addObject: filename];
248}
249}
250
251 // quick Enumeration
252 // for (NSString * filename in direnum)
253 //{
254 // if ([filename pathExtension] isw.tostring: @ "jpg"]) {
255 // [files addObject: filename];
256 //}
257 //}
258 NSLog (@ "files: % @", files );
259
260 // Enumeration
261 NSEnumerator * filenum;
262 filenum = [files objectEnumerator];
263 while (filename = [filenum nextObject]) {
264 NSLog (@ "filename: % @", filename );
265}
266
267 // quick Enumeration
268 // for (id object in files)
269 //{
270 // NSLog (@ "object: % @", object );
271 //}
From Technical Achievements