引言

利用NSPredicate从数组搜索特定条件的元素:

  1. 筛选选中的规格数据
  2. 将含有日期属性的对象数组按照日期重新分组
  3. 城市搜索
  4. 多个过滤条件的拼接:银行列表的过滤
  5. 字符串数组的过滤(数组元素为系统的自有类型)
  6. Core Data的数据查询
  7. 使用谓词进行数据分组 (数组元素为自定义类型)

利用正则表达式进行匹配查找数据:

  1. 使用正则表达式对聊天记录的关键词进行监控
  2. NSPredicate在正则表达式的应用:限制UITextField只能输入金额的正则表达式(0. .00)

使用系统特定API进行数据查找来避免循环遍历数组:

  1. 利用NSPredicate从数组中筛选type=8的电子签名数据,避免遍历数组 certificateInfoList
  2. 使用makeObjectsPerformSelector 来避免循环遍历数组
  3. 使用enumerate开头的方法

I 从数组搜索特定条件的元素


 

III 避免循环遍历数组的案例

3.1 利用NSPredicate从数组中筛选type=8的电子签名数据,避免遍历数组 certificateInfoList


 

- (CRMcertificateInfoListDto *)getSignimgURLCRMcertificateInfoListDto{


NSPredicate* predicate = [NSPredicate predicateWithFormat:@"type == %@", [[NSNumber numberWithInteger:CRMcertificateInfoListDtotype4Sign] description]];




NSArray *arFiltered = [ self.certificateInfoList filteredArrayUsingPredicate:predicate];//以一定的条件(特定日期)过滤maTemp数组,即进行大数据搜索。
CRMcertificateInfoListDto *dto = nil;


if(arFiltered.count>0){

dto = arFiltered.firstObject;


}


return dto;

}

3.2 使用makeObjectsPerformSelector 来避免循环遍历数组

  • 使用makeObjectsPerformSelector 设置self.StorePayFeeRateListDto.payFeeRateList数组内部元素的属性settlementCycle (结算周期:0-T0 1-T1 默认0) 来避免循环便利数组
[self.StorePayFeeRateListDto.payFeeRateList makeObjectsPerformSelector:@selector(setSettlementCycle:) withObject:swichStr];
  • 循环遍历数组的代码
for (int i = 0; i < self.StorePayFeeRateListDto.payFeeRateList.count; i++) {//settlementCycle



NSMutableDictionary * mutDic = [[NSMutableDictionary alloc]initWithDictionary:_sectionArr[i]];




[mutDic setObject:swichStr forKey:@"settlementCycle"];


[_sectionArr setObject:mutDic atIndexedSubscript:i];




}

3.3 使用enumerate开头的方法

[objc] view plain copy
/* In the enumerate methods, the blocks will be invoked inside an autorelease pool, so any values assigned inside the block should be retained.
*/
- (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString * __nullable substring, NSRange substringRange, NSRange enclosingRange, BOOLBOOL *stop))block NS_AVAILABLE(10_6, 4_0);
- (void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOLBOOL *stop))block NS_AVAILABLE(10_6, 4_0);
  • 用来一行一行的读取一个txt文档的内容
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"girl" ofType:@"txt"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *fileStr = [[NSString alloc]initWithData:fileData encoding:NSUTF8StringEncoding];

//一行一行的读取
[fileStr enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOLBOOL * _Nonnull stop) {
NSLog(@"%@\n",line);
}];

// 一个字符一字符的读取
[fileStr enumerateSubstringsInRange:NSMakeRange(0, fileStr.length) options:NSStringEnumerationByWords usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOLBOOL * _Nonnull stop) {
NSLog(@"tmp111===%@",substring);
}];
  • 遍历支付密码的例子
//    一个字符一字符的读取
[fileStr enumerateSubstringsInRange:NSMakeRange(0, fileStr.length) options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {




NSLog(@"tmp111===%@",substring);

[m_textField appendPsw:substring];
[_pswView SetInputNum:_inputEncData.count];
[m_textField onChange];

}];
  • NSStringEnumerationByWords
[fileStr enumerateSubstringsInRange:NSMakeRange(0, fileStr.length) options:NSStringEnumerationByWords
usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {




NSLog(@"tmp111===%@",substring);

[m_textField appendPsw:substring];
[_pswView SetInputNum:_inputEncData.count];
[m_textField onChange];

}];

see also

​1、请输入正确的结算卡号【银行卡号有效性校验Luhn算法】-银行卡号码校验算法2、身份证校验:【校验年龄、校验是否符合身份证号生成规则】​


 OC中的 BOOL不如bool好用

BOOL表示 1是YES , 所以非1是NO. 而bool表示0是false,所以非0是true;


???? 联系作者: iOS逆向(公号:iosrev)


???? 作者简介: 博客专家认证????丨全站 Top 50、华为云云享专家认证????、iOS逆向公号号主


???? 简历模板、技术互助。关注我,都给你。