foundation-数组的遍历

来源:互联网 发布:哈工大 知乎 编辑:程序博客网 时间:2024/04/29 10:33
////  main.m//  foundation-NSArray////  Created by apple on 15/6/28.//  Copyright (c) 2015年 itcast. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"#pragma mark 数组的创建void array(){    //NSArray *arr = [NSArray array];    NSArray *arr1 = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];    unsigned long count = [arr1 count];    NSLog(@"%ld",count);}#pragma mark 数组常用方法void arrayUse(){    NSObject *obj = [[NSObject alloc]init];    NSArray *arr1 = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",obj, nil];    if ([arr1 containsObject:@"a"]) {        NSLog(@"包含了a");    }    NSString *last = [arr1 lastObject];    NSLog(@"last = %@",last);        NSString *str = [arr1 objectAtIndex:1];    NSLog(@"objectAtIndex = %@",str);        unsigned long index = [arr1 indexOfObject:@"c"];    NSLog(@"indexOfObject = %ld",index);}#pragma mark 数组的内存管理void memory(){    Student *stu1 = [[Student alloc]init];    Student *stu2 = [[Student alloc]init];    Student *stu3 = [[Student alloc]init];//    NSLog(@"stu1.count = %ld",[stu1 retainCount]);    NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3, nil];//    NSLog(@"stu1.count = %ld",[stu1 retainCount]);    NSLog(@"count = %ld",arr.count);}#pragma mark 给数组中的元素发送消息void arrayMessage(){    Student *stu1 = [Student student];    Student *stu2 = [Student student];    Student *stu3 = [Student student];    NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3,nil];    //让数组中的所有元素都调用test方法    //[arr makeObjectsPerformSelector:@selector(test)];    [arr makeObjectsPerformSelector:@selector(test2:) withObject:@"peter"];}#pragma mark 遍历数组void arrayFor(){    NSArray *arr = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];    unsigned long count = arr.count;    for (long i=0; i<count; i++) {        //id == void *(指向任何类型的指针)        id obj = [arr objectAtIndex:i];        NSLog(@"%ld-%@",i,obj);    }}#pragma mark 遍历数组2void arrayFor2(){    NSArray *arr = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];    unsigned long i = 0;    for(id obj in arr){        NSLog(@"%ld-%@",i,obj);        i++;    }}#pragma mark 遍历数组3void arrayFor3(){    NSArray *arr = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];    [arr enumerateObjectsUsingBlock:     //*stop在里面要改变stop的值,所以要传一个指针     ^(id obj, NSUInteger idx, BOOL *stop) {        NSLog(@"%ld-%@",idx,obj);         //如果索引为1就停止遍历         if(idx == 1){             //利用指针修改外面BOOL变量的值             *stop = YES;         }     }];}#pragma mark 遍历数组3void arrayFor4(){    NSArray *arr = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];    //获取数组的迭代器    NSEnumerator *enumerator = [arr objectEnumerator];    //反序迭代器    enumerator = [arr reverseObjectEnumerator];    //取出所有对象(没有遍历过的)    NSArray *arr2 = [enumerator allObjects];    NSLog(@"obj=%@",arr2);        //获取下一个元素    NSObject *obj = nil;    while (obj = [enumerator nextObject]) {        NSLog(@"obj=%@",obj);    }}int main(int argc, const char * argv[]) {    @autoreleasepool {        arrayFor4();    }    return 0;}

0 0
原创粉丝点击