此文是对数组NSArray与NSMutableArray知识点的总结,主要是一些常见的操作,别外一些操作见其相应的文档,下面的代码部分还运用的第三方插件BlocksKit相结合;
1:NSArray一些常见操作
NSArray *array=[NSArray array]; //增加单个 //array=[NSArray arrayWithObject:@"wujy"]; //增加多个 array=[NSArray arrayWithObjects:@"VEVb",@".com",nil]; NSInteger arrayCount=array.count; NSLog(@"当前array数组个数为:%ld",arrayCount); int i=0; for (id obj in array) { NSLog(@"当前第%d个为%@",i,obj); i++; } //常用的数组操作 NSString *lastString=[array lastObject]; NSLog(@"最后一个对象的值为:%@",lastString); NSString *firstString=[array firstObject]; NSLog(@"第一个对象的值为:%@",firstString); NSString *indexString=[array objectAtIndex:1]; NSLog(@"第二个对象的值为:%@",indexString); NSInteger indexInt=[array indexOfObject:@"VEVb"]; NSLog(@"返回索引的位置:%ld",indexInt); //将字符串转化成数组 NSString *arrayString=@"a,b,c,d"; NSArray *newArray=[arrayString componentsSeparatedByString:@","]; for (id obj in newArray) { NSLog(@"当前字符串转化为%@",obj); } //判断数组是否存在元素 if ([newArray containsObject:@"c"]) { NSLog(@"存在字母c的元素"); } else { NSLog(@"不存在字母c的元素"); } //简便创建 NSArray *twoArray=[NSArray arrayWithObjects:@1,@2,@3,@4,@5,nil]; //迭代器遍历 reverSEObjectEnumerator数组元素从后向前访问 NSEnumerator *arrayenumerator=[twoArray reverseObjectEnumerator]; id obj=nil; while (obj=[arrayenumerator nextObject]) { NSLog(@"当前值为:%d",[obj intValue]); } //利用BlocksKit插件进行遍历 NSArray *frArray=[NSArray arrayWithObjects:@(0.2),@(0.5),@(0.9),nil]; [frArray bk_each:^(id obj) { NSLog(@"%@",obj); }]; //跟数组项进行对比,若不存在则为空 存在则为符合的值 id found=[frArray bk_match:^BOOL(id obj) { BOOL match=([obj floatValue]==0.5)?YES:NO; return match; }]; NSLog(@"有符合的值:%@",found); id notFound=[frArray bk_match:^BOOL(id obj) { BOOL match=([obj floatValue]==0.7)?YES:NO; return match; }]; NSLog(@"没有符合的值(nil):%@",notFound); //过滤数组 NSArray *selectFound=[frArray bk_select:^BOOL(id obj) { BOOL match=([obj floatValue]>0.3)?YES:NO; return match; }]; NSLog(@"当前过滤array数组个数为:%ld",selectFound.count); for (id obj in selectFound) { NSLog(@"当前符合为%@",obj); } //过滤数组 取相反 NSArray *rejectFound=[frArray bk_reject:^BOOL(id obj) { BOOL match=([obj floatValue]>0.3)?YES:NO; return match; }]; NSLog(@"当前取反array数组个数为:%ld",rejectFound.count); for (id obj in rejectFound) { NSLog(@"当前符合为(0.2符合)%@",obj); } //遍历数组 对每个值都进行修改 组成新的数组 NSArray *mapFound = [array bk_map:^id(id obj) { return [obj stringByAppendingString:@".png"]; }]; for (id obj in mapFound) { NSLog(@"当前map %@",obj); } //合并数组的各项值 //进行数组的合并 中间可以加分格符之类 (这边是前面加的一个|;合并后的字符串为:|VEVb.com) NSString *concentrated = [array bk_reduce:@"|" withBlock:^id(id sum, id obj) { return [sum stringByAppendingString:obj]; }]; NSLog(@"合并后的字符串为:%@",concentrated); //相加 还有一个为整数型 bk_reduceInteger float value = [frArray bk_reduceFloat:0 withBlock:^CGFloat(CGFloat result, id obj) { return result + [obj floatValue]; }]; NSLog(@“合并后的frArray值为:%f",value);
2:NSMutableArray一些常见的操作
//从array转nsmutablearray NSArray *array=[[NSArray alloc] initWithObjects:@"wujy",@"VEVb",nil]; NSMutableArray *mutableArray=[NSMutableArray arrayWithArray:array]; //创建NSMutableArray NSMutableArray *newMutableArray=[NSMutableArray array]; [newMutableArray addObject:@"a"]; [newMutableArray addObjectsFromArray:[[NSArray alloc] initWithObjects:@"b",@"c",@"d",@"e",nil]]; //迭代器遍历 reverseObjectEnumerator数组元素从后向前访问 NSEnumerator *arrayenumerator=[newMutableArray reverseObjectEnumerator]; id obj=nil; while (obj=[arrayenumerator nextObject]) { NSLog(@"当前值为:%@",obj); } //创建NSMutableArray NSMutableArray *capacityArray=[[NSMutableArray alloc] initWithCapacity:5]; [capacityArray addObjectsFromArray:[[NSArray alloc] initWithObjects:@1,@2,@3,nil]]; NSLog(@"删除前(1,2,3):%@",capacityArray); //删除指定的元素 [capacityArray removeObject:@3]; NSLog(@"删除后(1,2):%@",capacityArray); //插入 [capacityArray insertObject:@4 atIndex:2]; NSLog(@"插入后的(1,2,4)%@",capacityArray); //插入多个 NSIndexSet *set=[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)]; [capacityArray insertObjects:[[NSArray alloc] initWithObjects:@5,@6,nil] atIndexes:set]; NSLog(@"插入多个以后的(5,6,1,2,4):%@",capacityArray); //Blocks操作 //过滤 [capacityArray bk_performSelect:^BOOL(id obj) { BOOL match=([obj intValue]>2)?YES:NO; return match; }]; NSLog(@"过滤后的数组值为(5,6,4):%@",capacityArray); //取反过滤 [capacityArray bk_performReject:^BOOL(id obj) { BOOL match=([obj intValue]>5)?YES:NO; return match; }]; NSLog(@"过滤取反后的数组值为(5,4):%@",capacityArray); //对数组的值进行操作 [capacityArray bk_performMap:^id(id obj) { return [NSNumber numberWithInt:[obj intValue]+1]; }]; NSLog(@"map后的数组值为(6,5):%@",capacityArray);
新闻热点
疑难解答