zoukankan      html  css  js  c++  java
  • 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]) {  }

  • 相关阅读:
    tp框架实现ajax
    tp框架的增删改查
    tp框架获取常量信息、方法、命名空间
    tp框架,访问方式、空方法
    tp基础,文件存储路径
    缓存
    CMS系统存储路径
    Smarty模版引擎的原理
    php的empty(),trim(),strlen()方法
    PHP 流程管理
  • 原文地址:https://www.cnblogs.com/yingkong1987/p/3387937.html
Copyright © 2011-2022 走看看