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

我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章