NSPredicate 條件查詢 過慮

2021-06-16 11:08:16 字數 1926 閱讀 3091

coredata, nspredicate用於查詢和過濾

在sql中作為查詢條件通常用where,但在coredata中作為查詢條件就可以用到nspredicate.

nspredicate 不單可以和coredata中的fetchrequest 配合使用。也可以與nsarray配合使用。

1、>,<,>=,<=,= 比較運算子。

如:nspredicate * qcondition= [nspredicate predicatewithformat:@"salary >= 10000"];

2、字串操作(包含):beginswith、endswith、contains

如:@"employee.name beginswith[cd] '李'" //姓李的員工

@"employee.name endswith[c] '夢'"   //以夢結束的員工

@"employee.name contains[d] '宗'"   //包含有"宗"字的員工

注:[c]不區分大小寫[d]不區分發音符號即沒有重音符號[cd]既不區分大小寫,也不區分發音符號。

3、範圍:in   ,bwteen

如:@"salary bwteen "

@"em_dept in '開發'"

4、自身:self,這個只針對字元陣列起作用。

如:nsarray * test = =[nsarray arraywithobjects: @"guangzhou", @"beijing", @"shanghai", nil];

@"self='beijing'"

5、萬用字元:like

like 使用?表示乙個字元,*表示多個字元,也可以與c、d 連用。

如:@"car.name like '?he?'" //四個字元中,中間為he

@"car.name like '*jp'"   //以jp結束

6、正規表示式:matches

如:nsstring *regex = @"^e.+e$";//以e 開頭,以e 結尾的字元。

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

if([pre evaluatewithobject: @"employee"])else

7、邏輯運算子:and、or、not

如:@"employee.name = 'john' and employee.age = 28"

8、佔位符:

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

nsdictionary *dic=[nsdictionary dictionarywithobjectsandkeys:

@"name1", @"name",nil];

nspredicate *pre=[pretemplate predicatewithsubstitutionvariables: dic];

佔位符就是字典物件裡的key,因此你可以有多個佔位符,只要key 不一樣就可以了。

對陣列過濾:

如:nsmutablearray *carscopy = [carsmutablecopy];

[carscopyfilterusingpredicate: predicate];//filterusingpredicate和nsmutablearray構成新陣列。

nslog (@"%@", carscopy);

predicate = [nspredicatepredicatewithformat:@"engine.horsepower > %d", 50];

results = [cars filteredarrayusingpredicate: predicate];

nslog (@"%@", results);

NSPredicate查詢日期的問題

查詢日期的時候日期可以根據引數傳進去,但不能在字串中傳參後在作為查詢條件 簡單比較以下兩段 nsdate date1 nsdate date nsdate date2 date1 request.predicate nspredicate predicatewithformat studydate ...

NSPredicate查詢日期的問題

查詢日期的時候日期可以根據引數傳進去,但不能在字串中傳參後在作為查詢條件 簡單比較以下兩段 nsdate date1 nsdate date nsdate date2 date1 request.predicate nspredicate predicatewithformat studydate ...

條件查詢 後台查詢條件

將要查詢的條件放到乙個物件裡,然後查詢的字段放到物件中 下面上 布局部分 要查詢的表單部分 重 置查詢按鈕 查 詢c that.getlist 邏輯 部分查詢然後,因為會涉及到獲取時間的列表,這裡做了處理,上一節也有說過,然後賦值給查詢物件。獲取員工資料 getlist else that.axio...