NSPredicate用法和詳解

1. 基本用法

###1.1 比較運算符>,<,==,>=,<=,!= 以下代碼所示是比較對象car中屬性carName值是否等於BaoMa. 若是是NSInteger類型變量,也能夠用於比較大小. 若是是查找一個字符串中子串範圍, 須要用正則表達式.正則表達式

若是是比較字符串, 須要這麼寫SELF == 'i love you'", 若是是比較數字大小:"licheng > 100"數組

NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"carName == 'BMW'"];
BOOL match = [predicate1 evaluateWithObject:car];
if (match) {
    NSLog(@"match");
}else{
    NSLog(@"not match");
}

1.2 數組篩選

使用方法以下代碼所示, 經過filteredArrayUsingPredicate:這個方法篩選出符合條件的每個對象,而後會放到一個新的數組裏面, 能夠對數組進行相應的處理.lua

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"mileage > 100"];
NSArray *results = [cars filteredArrayUsingPredicate:predicate];
NSLog(@"resules = %@", results);

1.3 字符串自己:SELF

例:@「SELF == ‘APPLE’"code

2. 強大的數組運算符

predicate = [NSPredicate predicateWithFormat:  
                 @"engine.horsepower BETWEEN { 50, 200 }"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
     
    NSArray *betweens = [NSArray arrayWithObjects:  
                         [NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil];  
    predicate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN %@", betweens];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
    predicateTemplate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN $POWERS"];  
    varDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @"POWERS", nil];  
    predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
  
//IN運算符  
    predicate = [NSPredicate predicateWithFormat: @"name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", [results valueForKey: @"name"]);  
    predicate = [NSPredicate predicateWithFormat: @"SELF.name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", [results valueForKey: @"name"]);  
     
    names = [cars valueForKey: @"name"];  
    predicate = [NSPredicate predicateWithFormat: @"SELF IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];  
    results = [names filteredArrayUsingPredicate: predicate];//這裏限制了SELF的範圍  
    NSLog (@"%@", results);  
//BEGINSWITH,ENDSWITH,CONTAINS  
  
//附加符號,[c],[d],[cd],c表示不區分大小寫,d表示不區分發音字符,cd表示什麼都不區分  
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'Bad'"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  

    
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'HERB'"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
     
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH[cd] 'HERB'"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
  
//LIKE運算符(通配符)  
    predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '*er*'"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
     
    predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '???er*'"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);
相關文章
相關標籤/搜索