OC學習篇之---謂詞(NSPredicate) 分類: IOS 2014-12-14 10:39 2078人閱讀 評論(0) 收藏

在前一篇文章中咱們介紹了OC中一個重要技術通知:http://blog.csdn.net/jiangwei0910410003/article/details/41923401,今天咱們在來看一下OC中給咱們提供的一個技術:謂詞(NSPredicate)數據庫

OC中的謂詞操做是針對於數組類型的,他就比如數據庫中的查詢操做,數據源就是數組,這樣的好處是咱們不須要編寫不少代碼就能夠去操做數組,同時也起到過濾的做用,咱們能夠編寫簡單的謂詞語句,就能夠從數組中過濾出咱們想要的數據。很是方便。在Java中是沒有這種技術的,可是有開源的框架已經實現了此功能。數組


下面來看一下具體的例子吧:框架

Person.h測試

//
//  Person.h
//  46_NSPredicate
//
//  Created by jiangwei on 14-10-18.
//  Copyright (c) 2014年 jiangwei. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property NSString *name;
@property NSInteger age;

+ (id)personWithName:(NSString *)name andAge:(NSInteger)age;

@end


Person.m

//
//  Person.m
//  46_NSPredicate
//
//  Created by jiangwei on 14-10-18.
//  Copyright (c) 2014年 jiangwei. All rights reserved.
//

#import "Person.h"

@implementation Person

+ (id)personWithName:(NSString *)name andAge:(NSInteger)age{
    Person *person = [[Person alloc] init];
    person.name = name;
    person.age = age;
    return person;
}

- (NSString *)description{
    NSString *s =[NSString stringWithFormat:@"name=%@,age=%ld",_name,_age];
    return s;
}

@end
咱們在Person類中定義屬性,還有一個產生對象的類方法,同時重寫了description方法,用於打印結果


測試方法spa

main.m.net

//
//  main.m
//  46_NSPredicate
//
//  Created by jiangwei on 14-10-18.
//  Copyright (c) 2014年 jiangwei. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

//謂詞,指定過濾器的條件,將符合條件的對象保留下來
//通常用謂詞過濾數組中指定的元素
int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        NSArray *persons = [NSArray arrayWithObjects:
                            [Person personWithName:@"mac" andAge:20],
                            [Person personWithName:@"1" andAge:30],
                            [Person personWithName:@"2" andAge:40],
                            [Person personWithName:@"3" andAge:50],
                            [Person personWithName:@"4" andAge:60],
                            [Person personWithName:@"5" andAge:70],
                            [Person personWithName:@"6" andAge:20],
                            [Person personWithName:@"7" andAge:40],
                            [Person personWithName:@"8" andAge:60],
                            [Person personWithName:@"9" andAge:40],
                            [Person personWithName:@"0" andAge:80],
                            [Person personWithName:@"10" andAge:90],
                            [Person personWithName:@"1" andAge:20]];
        
        //年齡小於30
        //定義謂詞對象,謂詞對象中包含了過濾條件
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
        //使用謂詞條件過濾數組中的元素,過濾以後返回查詢的結果
        NSArray *array = [persons filteredArrayUsingPredicate:predicate];
        NSLog(@"filterArray=%@",array);
        
        //查詢name=1的而且age大於40
        predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
        array = [persons filteredArrayUsingPredicate:predicate];
        NSLog(@"filterArray=%@",array);
        
        //in(包含)
        predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];
        
        //name以a開頭的
        predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
        //name以ba結尾的
        predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
        
        //name中包含字符a的
        predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
        
        //like 匹配任意多個字符
        //name中只要有s字符就知足條件
        predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
        //?表明一個字符,下面的查詢條件是:name中第二個字符是s的
        predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
        
        
        
    }
    return 0;
}
首先咱們看到,咱們初始化了必定大小的數組。

而後咱們就可使用NSPredicate類進行過濾操做了code


一、查詢數組中年齡小於30的對象orm

//年齡小於30
//定義謂詞對象,謂詞對象中包含了過濾條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
//使用謂詞條件過濾數組中的元素,過濾以後返回查詢的結果
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"filterArray=%@",array);
首先創立一個過濾條件:

//定義謂詞對象,謂詞對象中包含了過濾條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
這裏面操做很簡單的:@"age<%d",這個age是Person的屬性名,%d至關於佔位符,而後後面用參數替換便可

而後進行過濾操做,返回一個過濾以後的數組對象對象

//使用謂詞條件過濾數組中的元素,過濾以後返回查詢的結果
NSArray *array = [persons filteredArrayUsingPredicate:predicate];


二、查詢name=1而且age大於40的集合

//查詢name=1的而且age大於40
predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
array = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"filterArray=%@",array);
固然咱們也可使用&&進行多條件過濾


三、包含語句的使用blog

//in(包含)
predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];


四、指定字符開頭和指定字符結尾,是否包含指定字符

//name以a開頭的
predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
//name以ba結尾的
predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];

//name中包含字符a的
predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];


五、like進行匹配多個字符

//like 匹配任意多個字符
//name中只要有s字符就知足條件
predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
//?表明一個字符,下面的查詢條件是:name中第二個字符是s的
predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];


總結

這一篇就介紹了OC中經常使用的技術:謂詞的使用,他用起來很方便的,並且也沒什麼難度,和咱們當初在操做數據庫的時候很想,可是他對咱們進行過濾操做提供了很大的便捷。

相關文章
相關標籤/搜索