1.判读数组中是否包含某个元素,不包含就添加进去
for (int i = 0; i < _currentDataArr.count; i++) {
GuideModel *model = _currentDataArr[i];
//不包含则添加 印度 日本 ...
if(![_countryNameArr containsObject:model.country_name_cn]){
//NSLog(@"%@",model.country_name_cn);
[_countryNameArr addObject:model.country_name_cn];
}
}
2.获取某个元素的下标
NSInteger index = [_imageViewArr indexOfObject:myImageView];
//从图片数组中删除
[_imageViewArr removeObject:myImageView];
NSUInteger index = [array0 indexOfObject:@"two"];
NSLog(@"index = %lu",index);
NSRange range = NSMakeRange(0, 2);
//查找某个范围内,某个元素对应的下标
NSUInteger index2 = [array0 indexOfObject:@"two" inRange:range];
NSLog(@"index2 = %lu",index2);
3.字符串的分割
NSString *str0 = @"mon*tue wed+thu fri sat #sun";
//1.用给定参数中的字符串来对str0进行分割
NSArray *array0 = [str0 componentsSeparatedByString:@" #"];
NSLog(@"array0 = %@",array0);
//2.用多个字符来分割
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@" #*+"];
NSArray *array1 = [str0 componentsSeparatedByCharactersInSet:set];
NSLog(@"array1 = %@",array1);
for (int i = 0; i < [array1 count]; i++) {
NSString *str = [array1 objectAtIndex:i];
//如果不是空串 --> 打印
if ([str length] > 0) {
NSLog(@"str = %@",str);
}
}
4.将BOOL值存入数组中
//保存用户信息
- (void)saveUser:(NSString *)userName passwd:(NSString *)passwd {
//获取NSUserDefaults
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
//NSArray *userInfo = @[userName,passwd,[NSNumber numberWithBool:_isManager]];
NSArray *userInfo = @[userName,passwd,@(_isManager)];
//保存数据 用户名 作为key 数组整体作为value
[ud setObject:userInfo forKey:userName];
//立即同步到磁盘
[ud synchronize];
}
5.新属性
NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"two", nil];
//给一个数组增加一个元素 返回一个新的数组地址
NSArray *newArr = [array arrayByAddingObject:@"three"];
NSLog(@"arr:%p->%@",array,array);
NSLog(@"newArr:%p->%@",newArr,newArr);
//给newArr 追加一个数组 放入新的数组 并返回新的数组地址
NSArray *newArr2 = [newArr arrayByAddingObjectsFromArray:array];
NSLog(@"newArr2:%@",newArr2);
//获取指定元素的下标(返回的找到的第一个元素)
NSInteger index = [newArr2 indexOfObject:@"one1"];
if (index == NSNotFound) {
NSLog(@"没有找到");
}else {
NSLog(@"index:%ld",index);
}
//在指定范围内查找某个元素的下标
index = [newArr2 indexOfObject:@"one" inRange:NSMakeRange(1, 4)];
NSLog(@"index:%ld",index);
NSMutableArray *mArr = [NSMutableArray arrayWithObjects:@"oc",@"ui",@"ios",@"android", nil];
//把array 数组元素 增加 可变数组mArr
[mArr addObjectsFromArray:array];
NSLog(@"mArr:%@",mArr);
//mArr->oc ui ios android one two
//newArr one two three
//跟newArr 数组中的元素删除mArr中相同的元素
[mArr removeObjectsInArray:newArr];
NSLog(@"mArr:%@",mArr);
//删除指定范围内的元素
[mArr removeObjectsInRange:NSMakeRange(1, 2)];
NSLog(@"mArr:%@",mArr);