zoukankan      html  css  js  c++  java
  • NSArray和NSMutableArray的详解

    数组中不能存放基本数据类型,必须存放对象,因此如果要存放基本数据类型,先进行NSTimer封装

    NSArray的用法:

    第一、初始化

    NSArray *firstArray=[[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
            NSArray *secondArray=[NSArray arrayWithArray:firstArray];
    
    

    第二、获取元素个数和访问

            NSLog(@"the number is %ld",[secondArray count]);
            NSLog(@"the value is %@",[secondArray objectAtIndex:2]);
    


    第三、追加数据元素

            NSArray *thirdArray=[firstArray arrayByAddingObjectsFromArray:secondArray];
    


    第四、数组转化为字符串

     

            NSString *str=[firstArray componentsJoinedByString:@".."];
            NSLog(@"the number is %@",str);
    


    第五、判断是否包含字符串

     

            NSArray *firstArray=[[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
            NSLog(@"has value %d",[firstArray containsObject:@"two"]);
            NSLog(@"has value %ld",[firstArray indexOfObject:@"two"]);
            NSLog(@"the last object is %@",[firstArray lastObject]);
    


    NSMutalbeArray 的用法-

    第一、基本的增删改

     

         NSMutableArray *mutableArr=[NSMutableArray arrayWithCapacity:4];
            [mutableArr addObject:@"hello"];
            [mutableArr addObject:@"hello"];
            [mutableArr addObject:@"hello"];
    
            [mutableArr addObject:@"richard"];
            [mutableArr insertObject:@"yang" atIndex:1];
            NSLog(@"%@",mutableArr);
            [mutableArr removeObject:@"hello"];
            [mutableArr removeObjectAtIndex:0];
            [mutableArr removeLastObject];
            NSLog(@"%@",mutableArr);


    第二、替换操作

     

            [mutableArr replaceObjectAtIndex:0 withObject:@"kaixin"];
    


    第三、遍历

     

            NSMutableArray *mutableArr=[NSMutableArray arrayWithCapacity:4];
            [mutableArr addObject:@"hello"];
            [mutableArr addObject:@"hello"];
            [mutableArr addObject:@"hello"];
            for(int index=0;index<[mutableArr count];index++)
            {
                NSLog(@"the val is %@",[mutableArr objectAtIndex:index]);
            }
            for(NSString *str in mutableArr)
            {
                NSLog(@"%@",str);
            }
            for (id str in mutableArr) {
                NSLog(@"%@",str);
            }
    



  • 相关阅读:
    .Net时间计算函数,统计某一天是一年的第几周,这一周从哪天开始到哪天结束
    1分钟搞定超慢SQL
    网站
    舞台
    相见欢
    一套完整系统对人生的意义
    2015/08/15心情
    Linux下压缩某个文件夹(文件夹打包)
    init进程 && 解析Android启动脚本init.rc && 修改它使不启动android && init.rc中启动一个sh文件
    andriod系统裁剪心得
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3364686.html
Copyright © 2011-2022 走看看