码迷,mamicode.com
首页 > 编程语言 > 详细

oc中数组的学习过程

时间:2015-05-29 22:56:10      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

        NSArray *aa = [[NSArray alloc]init];
        NSArray *aaa = [NSArray array];
        //声明单个内容数组
        NSArray *aaaa = [NSArray arrayWithObject:@"sd"];
        //声明多个内容数组
        NSArray *arr = [NSArray arrayWithObjects:@"SDS",@"Dsad", nil];
        //数组的长度
        NSLog(@"%zi",arr.count);
        //数组中某个下标对应的对象
        NSLog(@"%@",[arr objectAtIndex:1]);
        //数组中最后一个对应的对象
        NSLog(@"%@",[arr lastObject]);
        //数组中某个元素对应的下标
        NSLog(@"%zi",[arr indexOfObject:@"Dsad"]);
        //写入文件
        NSString *path = @"/Users/XuLee/Desktop/oc/abc.txt";
        [arr writeToFile:path atomically:YES];
        //读文件必须是特定格式
        NSString *ff = [NSArray arrayWithContentsOfFile:path];
        //把数组的对象中间加一个东西连接成一个字符串
        NSString *fg = [arr componentsJoinedByString:@"-"];
        //数组的遍历
        //普通遍历方式
        for (int i = 0; i < arr.count; i++) {
            id obj = [arr objectAtIndex:i];
            NSLog(@"%@",obj);
        }
        //快速遍历
        for (id obj in arr ) {
            NSLog(@"%@",obj);
        }
        //代码块遍历
        [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
        // obj为元素 idx 为下标 stop相当于break  stop为yes时 循环会结束
        {
            NSLog(@"%@",obj);
            //stop 赋值形式为 *stop = yes or no
        }];
        //迭代器遍历
        //将数组中各个元素装入迭代器中
        NSEnumerator * vv = [arr objectEnumerator];
        id obj = nil;
        while (obj = [vv nextObject]) {
            NSLog(@"%@",obj);
        }
        //反向迭代
        NSEnumerator *vvv = [arr reverseObjectEnumerator];
        while (obj = [vvv nextObject]) {
            NSLog(@"%@",obj);
        }
        //让数组统一调用某方法
        
        Student *stu1 = [[Student alloc]init];
        stu1.name = @"fff";
        Student *stu2 = [[Student alloc]init];
        stu2.name = @"hhh";
        NSArray *aee = [NSArray arrayWithObjects:stu1,stu2,nil];
        [aee makeObjectsPerformSelector:@selector(sayHi)];
        
        //让数组统一调用某一带参数的方法
        
        [aee makeObjectsPerformSelector:@selector(study:) withObject:@"math"];
        
-(NSString *)description
{
    NSString *aa = [NSString stringWithFormat:@"%@",_name];
    return aa;
}
-(void)sayHi
{
    NSLog(@"my name %@",_name);
}
-(void)study:(NSString *)ss
{
    NSLog(@"%@ study %@",_name,ss);
}
@property (nonatomic,strong)NSString *name;
-(void)sayHi;
-(void)study:(NSString *)ss;

 

oc中数组的学习过程

标签:

原文地址:http://www.cnblogs.com/0error/p/4539459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!