文章标题

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:
Test1

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:
Test2

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:
Test3

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:
Test4

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:
Test5

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:
Test6

Test 7 (componentsSeparatedByString)


 NSString *string =[[NSString alloc]initWithString:@"One,Two,Three,Four"];
            NSLog(@"string:%@",string);
            NSArray * array =[ string componentsSeparatedByString:@","];
            NSLog(@"array:%@",array);

Result 7:
Test7

Test 8 (componentsJoinedByString)


NSArray  *array =[[NSArray alloc]initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
        NSString *string =[array componentsJoinedByString:@","];
        NSLog(@"string:%@",string);

Result 8:
Test8

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:
Test9

Test 10 (removeObjectAtIndex)


 //-(void)removeObjectAtIndex:(unsigned)index;
            NSMutableArray *array = [NSMutableArray arrayWithObjects:@"one",@"two",@"Three",nil];
            NSLog(@"array:%@",array);
            [array removeObjectAtIndex:1];
            NSLog(@"array:%@",array);

Test10

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:
Test11

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);
            }

Test12

Test 13


 NSMutableArray *array =[NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];
            for(NSString *string in array){
                NSLog(@"string:%@",string);
            }

Result 13:
Test13

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:
Test14

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:
Test15

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:
Test16

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:
Test17

Ref:
http://blog.sina.com.cn/s/blog_65de545001016vpj.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值