NSArray and NSDictionary

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

 

posted on 2013-03-19 03:27  leosen  阅读(141)  评论(0)    收藏  举报