• NSPredicate 应用 --数组如何一键去重,如何一行代码筛选,请慢慢看来


    1.去重

    NSArray * uniqueVarValueArray= [origArray valueForKeyPath:@"@distinctUnionOfObjects.VarKey"];

     

     

    2.判断Array中是否包含某一规则的对象,并返回一个数组: 

    NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", regex]; 

    并调用:- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; 方法即可。 

     

     

    3. 获得一个数组中某些对象除外的数组: 

    NSPredicate *notPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter2];

    且还是要调用- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; 方法。 

     

    4.找出某个范围内的对象:

    创建如下Predicate (这里可以用到所有的比较操作符) NSPredicate *pre = [NSPredicate predicateWithFormat:@"self.*** < 5"]; 

    并调用:- (BOOL)evaluateWithObject:(id)object;方法。 

    在这里啰嗦一句,如果只是在数组中查找是否存在对象时用indexOfObject,如果不存在则返回为NSNotFound. 

     

     

    5. 判断字符串首字母是否为字母。 

     

    NSString *regex = @"[A-Za-z]+"

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; 

      if ([predicate evaluateWithObject:aString]) {  }

  • 相关阅读:
    htpasswd命令
    GitHub访问速度慢的解决方法
    easyui datagrid 首次不加载做法
    Excel日常操作
    补偿接口中循环一直执行sql的问题
    rabbitMq无法消费发送的q的问题
    Unicode与中文转换工具类方法(转)
    idea 一些插件配置
    线程安全的集合类、CopyOnWrite机制介绍(转)
    java websocket学习
  • 原文地址:https://www.cnblogs.com/yingkong1987/p/3387937.html
Copyright © 2020-2023  润新知