天天看點

iOS的查詢、過濾(NSPredicate)

<a target="_blank" href="http://blog.csdn.net/z251257144/article/details/8576308">原文:http://www.2cto.com/kf/201208/150608.html</a>

<a target="_blank" href="http://blog.csdn.net/iscape/article/details/7318021">參考:http://blog.csdn.net/iscape/article/details/7318021</a>

<a target="_blank" href="http://blog.csdn.net/zhulei1018/article/details/6777220">參考:http://blog.csdn.net/zhulei1018/article/details/6777220</a>

首先舉一個例子:

比對9-15個由字母/數字組成的字元串的正規表達式:

    nsstring * regex = @"^[a-za-z0-9]{9,15}$";

    nspredicate *pred = [nspredicate predicatewithformat:@"self matches %@", regex];

    bool ismatch = [pred evaluatewithobject:txtfldphonenumber.text];

cocoa用nspredicate描述查詢的方式,原理類似于在資料庫中進行查詢

用between,in,beginwith,endwith,contains,like這些謂詞來構造nspredicate,必要的時候使用self直接對自己進行比對

//基本的查詢  

nspredicate *predicate; 

predicate = [nspredicate predicatewithformat: @"name == 'herbie'"]; 

    bool match = [predicate evaluatewithobject: car]; 

    nslog (@"%s", (match) ? "yes" : "no"); 

//在整個cars裡面循環比較  

    predicate = [nspredicate predicatewithformat: @"engine.horsepower &gt; 150"]; 

    nsarray *cars = [garage cars]; 

    for (car *car in [garage cars]) { 

        if ([predicate evaluatewithobject: car]) { 

            nslog (@"%@", car.name); 

        } 

    } 

//輸出完整的資訊  

    nsarray *results; 

    results = [cars filteredarrayusingpredicate: predicate]; 

    nslog (@"%@", results); 

//含有變量的謂詞  

    nspredicate *predicatetemplate = [nspredicate predicatewithformat:@"name == $name"]; 

    nsdictionary *vardict; 

    vardict = [nsdictionary dictionarywithobjectsandkeys: 

               @"herbie", @"name", nil]; 

    predicate = [predicatetemplate predicatewithsubstitutionvariables: vardict]; 

    nslog(@"snorgle: %@", predicate); 

    match = [predicate evaluatewithobject: car]; 

  nslog (@"%s", (match) ? "yes" : "no"); 

//注意不能使用$variable作為路徑名,因為它值代表值  

    predicate = [nspredicate predicatewithformat: 

                 @"(engine.horsepower &gt; 50) and (engine.horsepower &lt; 200)"]; 

    nslog (@"oop %@", results); 

    predicate = [nspredicate predicatewithformat: @"name &lt; 'newton'"]; 

    nslog (@"%@", [results valueforkey: @"name"]); 

//強大的數組運算符  

                 @"engine.horsepower between { 50, 200 }"]; 

    nsarray *betweens = [nsarray arraywithobjects: 

                         [nsnumber numberwithint: 50], [nsnumber numberwithint: 200], nil]; 

    predicate = [nspredicate predicatewithformat: @"engine.horsepower between %@", betweens]; 

    predicatetemplate = [nspredicate predicatewithformat: @"engine.horsepower between $powers"]; 

    vardict = [nsdictionary dictionarywithobjectsandkeys: betweens, @"powers", nil]; 

//in運算符  

    predicate = [nspredicate predicatewithformat: @"name in { 'herbie', 'snugs', 'badger', 'flap' }"]; 

    predicate = [nspredicate predicatewithformat: @"self.name in { 'herbie', 'snugs', 'badger', 'flap' }"]; 

    names = [cars valueforkey: @"name"]; 

    predicate = [nspredicate predicatewithformat: @"self in { 'herbie', 'snugs', 'badger', 'flap' }"]; 

    results = [names filteredarrayusingpredicate: predicate];//這裡限制了self的範圍  

//beginswith,endswith,contains  

//附加符号,[c],[d],[cd],c表示不區分大小寫,d表示不區分發音字元,cd表示什麼都不區分  

    predicate = [nspredicate predicatewithformat: @"name beginswith 'bad'"]; 

    predicate = [nspredicate predicatewithformat: @"name beginswith 'herb'"]; 

    predicate = [nspredicate predicatewithformat: @"name beginswith[cd] 'herb'"]; 

//like運算符(通配符)  

    predicate = [nspredicate predicatewithformat: @"name like[cd] '*er*'"]; 

    predicate = [nspredicate predicatewithformat: @"name like[cd] '???er*'"]; 

//基本的查詢

nspredicate *predicate;

predicate = [nspredicate predicatewithformat: @"name == 'herbie'"];

    bool match = [predicate evaluatewithobject: car];

    nslog (@"%s", (match) ? "yes" : "no");

//在整個cars裡面循環比較

    predicate = [nspredicate predicatewithformat: @"engine.horsepower &gt; 150"];

    nsarray *cars = [garage cars];

    for (car *car in [garage cars]) {

        if ([predicate evaluatewithobject: car]) {

            nslog (@"%@", car.name);

        }

    }

//輸出完整的資訊

    nsarray *results;

    results = [cars filteredarrayusingpredicate: predicate];

    nslog (@"%@", results);

//含有變量的謂詞

    nspredicate *predicatetemplate = [nspredicate predicatewithformat:@"name == $name"];

    nsdictionary *vardict;

    vardict = [nsdictionary dictionarywithobjectsandkeys:

               @"herbie", @"name", nil];

    predicate = [predicatetemplate predicatewithsubstitutionvariables: vardict];

    nslog(@"snorgle: %@", predicate);

    match = [predicate evaluatewithobject: car];

  nslog (@"%s", (match) ? "yes" : "no");

//注意不能使用$variable作為路徑名,因為它值代表值

//謂詞字元竄還支援c語言中一些常用的運算符

    predicate = [nspredicate predicatewithformat:

                 @"(engine.horsepower &gt; 50) and (engine.horsepower &lt; 200)"];

    nslog (@"oop %@", results);

    predicate = [nspredicate predicatewithformat: @"name &lt; 'newton'"];

    nslog (@"%@", [results valueforkey: @"name"]);

//強大的數組運算符

                 @"engine.horsepower between { 50, 200 }"];

    nsarray *betweens = [nsarray arraywithobjects:

                         [nsnumber numberwithint: 50], [nsnumber numberwithint: 200], nil];

    predicate = [nspredicate predicatewithformat: @"engine.horsepower between %@", betweens];

    predicatetemplate = [nspredicate predicatewithformat: @"engine.horsepower between $powers"];

    vardict = [nsdictionary dictionarywithobjectsandkeys: betweens, @"powers", nil];

//in運算符

    predicate = [nspredicate predicatewithformat: @"name in { 'herbie', 'snugs', 'badger', 'flap' }"];

    predicate = [nspredicate predicatewithformat: @"self.name in { 'herbie', 'snugs', 'badger', 'flap' }"];

    names = [cars valueforkey: @"name"];

    predicate = [nspredicate predicatewithformat: @"self in { 'herbie', 'snugs', 'badger', 'flap' }"];

    results = [names filteredarrayusingpredicate: predicate];//這裡限制了self的範圍

//beginswith,endswith,contains

//附加符号,[c],[d],[cd],c表示不區分大小寫,d表示不區分發音字元,cd表示什麼都不區分

    predicate = [nspredicate predicatewithformat: @"name beginswith 'bad'"];

    predicate = [nspredicate predicatewithformat: @"name beginswith 'herb'"];

    predicate = [nspredicate predicatewithformat: @"name beginswith[cd] 'herb'"];

//like運算符(通配符)

    predicate = [nspredicate predicatewithformat: @"name like[cd] '*er*'"];

    predicate = [nspredicate predicatewithformat: @"name like[cd] '???er*'"];

繼續閱讀