星空网 > 软件开发 > 操作系统

谓词NSPredicate

cocoa中提供了NSPredicate类,指定过滤器的条件,将符合条件的对象保留下来.谓词筛选后的结果也是一个数组.
CoreData查找数据也是通过谓词.

谓词设置方法

//设置条件    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"age<5"];    //与    NSPredicate *predicate1=[NSPredicate predicateWithFormat:@"age<9&&age>5"];    //或    NSPredicate *predicate2=[NSPredicate predicateWithFormat:@"age<3||age>5"];    //占位符传参    NSPredicate *predicate3=[NSPredicate predicateWithFormat:@"age=%d",3];    //多条件    NSArray *arrayname=@[@"jack1",@"Tom7" ];    NSPredicate *predicate4=[NSPredicate predicateWithFormat:@"name in %@",arrayname];    //是否以某个字符开头,可以使用和与或    NSPredicate *predicate5=[NSPredicate predicateWithFormat:@"name beginswith 'j'"];    //是否以某个字符结尾,可以使用和与或    NSPredicate *predicate6=[NSPredicate predicateWithFormat:@"name endswith '9'"];    //contains 是否包含某个字符    NSPredicate *predicate7=[NSPredicate predicateWithFormat:@"name contains 'a'"];    //like 类似SQL中的模糊查找    NSPredicate *predicate8=[NSPredicate predicateWithFormat:@"name like '*a*'"];

搜索结果获得方法

//方法一:    for (person *p1 in array) {      //返回布尔值,根据布尔值可以判断符合要求的数据      BOOL result=[predicate evaluateWithObject:p1];      if (result) {        NSLog(@"%@",p1);      }    }    //方法二    //filterUsingPredicate,将过滤后的方法放入原方法中,数组是可变数组    [array filterUsingPredicate:predicate];    for (person *p1 in array) {      NSLog(@"%@",p1);    }    //方法三    //filteredArrayUsingPredicate,将过滤后的结果放入一个新的数组中    NSArray *array1=[array filteredArrayUsingPredicate:predicate8];    for (person *p1 in array1) {      NSLog(@"%@",p1);    }

 

 
 



原标题:谓词NSPredicate

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

AI代写年终总结掀热潮,AI原生应用的正确打开方式:https://www.kjdsnews.com/a/1701354.html
出海Weekly No.19 | 运动塑身衣在TikTok美区迎来“买买买”热潮:https://www.kjdsnews.com/a/1701355.html
平台又来抢钱啦!:https://www.kjdsnews.com/a/1701356.html
独立站海外营销指南:YouTube推广方式:https://www.kjdsnews.com/a/1701357.html
超日元!追美元!人民币创历史新高!:https://www.kjdsnews.com/a/1701358.html
独立站用paypal收款,客诉问题如何有效处理和规避?:https://www.kjdsnews.com/a/1701359.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流