zoukankan      html  css  js  c++  java
  • 关于NSpredicate

            /*

             简述:Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取。

             定义(最常用到的方法):

             

             NSPredicate这个类有点类似于数据库中的查询,是用于在一批内容中查询符合条件的子集,中文翻译成“谓词”。这个翻译实在让我感觉很别扭,虽然明知道和谓语这个词语没什么关系,但确实总让我感觉这是一个句子里面的成分。

             

             我们有些时候会有一个对象的数组或者集合,然后希望从里面找出符合条件的集合,类似于做一次过滤操作。比如我有一批照片,希望能找出所有某一天里面拍摄的内容。

             

             NSPredicate类的创建往往使用predicateWithFormat的方法,这个方法的使用有点类似于stringWithFormat方法。

             

             */

    //查询单词里面包含“ang”的字符串

            NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil];

            NSString *string = @"ang";

            NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string];

            NSLog(@"%@",[array filteredArrayUsingPredicate:pred]);

    //查找名字里面包含“王”的姓

            NSArray *array = [[NSArray alloc]initWithObjects:@"小王",@"王力",@"李丽",@"方方", nil];

            

            NSString *match = @"*王*";

            //查找姓王的名字,王字必须为首字则修改NSString *match=@"王*";

            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match];

            

            NSArray *results = [array filteredArrayUsingPredicate:predicate];

            for (id str in results) {

                NSLog(@"%@",str);

            }

  • 相关阅读:
    Oracle报错:ORA-01747: user.table.column, table.column 或列说明无效
    easyUI 比较时间大小
    五个在XML文档中预定义好的实体
    js截取字符串
    luogu2155 [SDOI2008]沙拉公主的困惑
    Codeforces Round #533 (Div. 2)题解
    luogu3327 [SDOI2015]约数个数和
    luogu3911 最小公倍数之和(莫比乌斯反演)
    luogu4449 于神之怒加强版(莫比乌斯反演)
    luogu3704 [SDOI2017]数字表格(莫比乌斯反演)
  • 原文地址:https://www.cnblogs.com/wuduo/p/5120720.html
Copyright © 2011-2022 走看看