Test 1 (initWithObjects)
NSArray * array = [[NSArray alloc]initWithObjects:@"one",@"two",@"Three",@"Four", nil];
//-(unsigned)count;
NSLog(@"array counter:%lu",[array count]);
//-(id)objectAtIndex:(unsigned)index;
NSLog(@"array index 2:%@",[array objectAtIndex:2]);
Result 1:
Test 2 (arrayWithArray)
NSArray *array1=[[NSArray alloc]init];
NSMutableArray * mutableArray = [[NSMutableArray alloc]init];
NSArray * array = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];
NSLog(@"array:%@",array);
mutableArray=[NSMutableArray arrayWithArray:array];
NSLog(@"MutableArray:%@",mutableArray);
array1= [NSArray arrayWithArray:array];
NSLog(@"array1:%@",array1);
Result 2:
Test 3 (objectAtIndex)
id obj;
NSMutableArray * newArray = [[NSMutableArray alloc]init];
NSArray *oldArray = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
for(int i=0 ; i<[oldArray count];++i){
obj=[[oldArray objectAtIndex:i]copy];
[newArray addObject:obj];
}
NSLog(@"newArray:%@",newArray);
Result 3:
Test 4 (addObject)
NSMutableArray *newArray =[[NSMutableArray alloc]init];
NSArray *oldArray =[NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
for(id obj in oldArray){
[newArray addObject:obj];//We can see ,The NSMutableArray can be change ....
}
NSLog(@"newArray:%@",newArray);
Result 4:
Test 5 (CFPropertyListCreateDeepCopy)
– (please do no use ARC in this case)
NSMutableArray *newArray =[[NSMutableArray alloc]init];
NSArray * oldArray = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",nil];
NSLog(@"oldArray:%@",oldArray);
newArray=(NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray,kCFPropertyListMutableContainers);
NSLog(@"newArray:%@",newArray);
Result 5:
Test 6 (sortUsingSelector & sortUsingSelector)
(copy array and sort it )
NSMutableArray *newArray =[[NSMutableArray alloc]init];
NSArray *oldArray =[NSArray arrayWithObjects:@"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];
NSLog(@"oldArray:%@",oldArray);
NSEnumerator * enumerator;
enumerator = [oldArray objectEnumerator];
id obj;
while(obj=[enumerator nextObject])
{
[newArray addObject:obj];
}
[newArray sortUsingSelector:@selector(compare:)];
NSLog(@"newArray:%@",newArray);
Result 6:
Test 7 (componentsSeparatedByString)
NSString *string =[[NSString alloc]initWithString:@"One,Two,Three,Four"];
NSLog(@"string:%@",string);
NSArray * array =[ string componentsSeparatedByString:@","];
NSLog(@"array:%@",array);
Result 7:
Test 8 (componentsJoinedByString)
NSArray *array =[[NSArray alloc]initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
NSString *string =[array componentsJoinedByString:@","];
NSLog(@"string:%@",string);
Result 8:
Test 9 (arrayWithCapacity & addObject)
NSArray *array;
array = [NSMutableArray arrayWithCapacity:20];
//-(void)addObject:(id)anObject;
NSMutableArray * mutableArray =[NSMutableArray arrayWithObjects:@"one",@"two",@"three",nil];
[mutableArray addObject:@"Four"];
NSLog(@"mutable:%@",mutableArray);
NSLog(@"mutable count:%lu",[mutableArray count]);
NSLog(@"array count:%lu",[array count]);
Result 9:
Test 10 (removeObjectAtIndex)
//-(void)removeObjectAtIndex:(unsigned)index;
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"one",@"two",@"Three",nil];
NSLog(@"array:%@",array);
[array removeObjectAtIndex:1];
NSLog(@"array:%@",array);
Test 11 (objectEnumerator)
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"one",@"Two",@"Three",nil];
NSEnumerator * enumerator;
enumerator = [array objectEnumerator];
id thingie;
while (thingie = [enumerator nextObject]){
NSLog(@"thigie:%@",thingie);
Result 11:
Test 12 (reverseObjectEnumerator)
//-(NSEnumerator*)reverseObjectEnumerator;
NSMutableArray * array =[NSMutableArray arrayWithObjects:@"one",@"two",@"Three",nil];
NSEnumerator * enumerator;
enumerator = [array reverseObjectEnumerator];
id object;
while(object = [enumerator nextObject]){
NSLog(@"object:%@",object);
}
Test 13
NSMutableArray *array =[NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];
for(NSString *string in array){
NSLog(@"string:%@",string);
}
Result 13:
Test 14 (initWithObjectsAndKeys)
//-(id)initWithObjectsAndKeys;
NSDictionary *dictionary = [[NSDictionary alloc]initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3", nil];
NSString *string =[dictionary objectForKey:@"One"];
NSLog(@"string:%@",string);
NSLog(@"dictionary:%@",dictionary);
Result 14:
Test 15 (removeObjectForKey)
NSMutableDictionary *dictionary =[NSMutableDictionary dictionary];
[dictionary setObject:@"One" forKey:@"1"];
[dictionary setObject:@"Two" forKey:@"2"];
[dictionary setObject:@"Three" forKey:@"3"];
[dictionary setObject:@"Four" forKey:@"4"];
NSLog(@"dictionary:%@",dictionary);
[dictionary removeObjectForKey:@"3"];
NSLog(@"dictionary:%@",dictionary);
Result 15:
Test 16 ( NSValue & CGRect & valueWithBytes)
NSMutableArray *array =[[NSMutableArray alloc]init];
NSValue *value;
CGRect rect = CGRectMake(0, 0, 320, 480);
value=[NSValue valueWithBytes:&rect objCType:@encode(CGRect)];
[array addObject:value];
NSLog(@"array:%@",array);
value = [array objectAtIndex:0];
[value getValue:&rect];
NSLog(@"value:%@",value);
Result 16:
Test 17
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * home;
home = @"../Users";
NSDirectoryEnumerator * direnum;
direnum =[fileManager enumeratorAtPath:home];
NSMutableArray * files =[[NSMutableArray alloc]init];
NSString *filename;
while (filename =[direnum nextObject]){
if ([[filename pathExtension]hasSuffix:@"jpg"]) {
[files addObject:filename];
}
}
for (NSString *filename in direnum) {
if ([[filename pathExtension]isEqualToString:@"jpg"]) {
[files addObject:filename];
}
}
NSLog(@"files:%@",files);
NSEnumerator * filenum;
filenum = [files objectEnumerator];
while (filename =[filenum nextObject]){
NSLog(@"filename:%@",filename);
}
for(id object in files){
NSLog(@"object:%@",object);
}
Result 17: