数组转字符串
- (NSString *)componentsJoinedByString:(NSString *)separator;
#pragma mark - 数组转字符串
NSArray *arr = @[@"1",@"2",@"3"];
NSString *str = [arr componentsJoinedByString:@","];
NSLog(@"%@", str);
字符串转数组
- (NSArray<NSString *> *)componentsSeparatedByString:(NSString *)separator;
#pragma mark - 字符串转数组
NSString *str1 = @"张三, 李四, 王五";
NSArray *array1 = [str1 componentsSeparatedByString:@","];
NSLog(@"%@", array1);
数组进行遍历
RAC
[array.rac_sequence.signal subscribeNext:^(id _Nullable x) {
}];
OC
1. for...in...{ }
2. [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
}];
filter 过滤 利用谓词根据一定条件从一个数组中过滤数据
NSPredicated谓词的使用
关键字:
1.比较: >,<,==,>=,<=,!=
2.范围: IN、BETWEEN
3.字符串相关:CONTAINS(包含)、 BEGINSWITH(以某个字符串开头)、ENDSWITH(以某个字符串结束)
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",text];
4.通配符: LIKE
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] '*er'"];
5.正则匹配: MATCHES
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name MATCHES 'Z.+e$'"];
6.组合查询 AND
NSArray * arr1 = @[@2,@3,@4,@5];
NSArray * arr2 = @[@4,@3,@2,@1];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF > 4",arr1];
NSArray *result = [arr1 filteredArrayUsingPredicate:predicate];
NSLog(@"arr1中大于4的数据:%@", result);
NSPredicate * predicate2 = [NSPredicate predicateWithFormat:@"SELF BETWEEN {3, 5}",arr1];
NSArray *result2 = [arr1 filteredArrayUsingPredicate:predicate2];
NSLog(@"arr1中3-5的数据:%@", result2);
NSPredicate * filterPredicate1 = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",arr1];
NSArray * filter1 = [arr2 filteredArrayUsingPredicate:filterPredicate1];
NSLog(@"只在arr2中的数据:%@", filter1);
NSPredicate * filterPredicate2 = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",arr2];
NSArray * filter2 = [arr1 filteredArrayUsingPredicate:filterPredicate2];
NSLog(@"只在arr1中的数据:%@",filter2);
NSMutableArray *array = [NSMutableArray arrayWithArray:filter1];
[array addObjectsFromArray:filter2];
NSLog(@"arr1、arr2中不同的数据:%@",array);
网友评论