iOS開發 NSPredicate 謂詞過濾

2021-07-11 08:31:17 字數 4649 閱讀 4233

一、根據模型條件過濾陣列

nsmutablearray *marr = [[nsmutablearray alloc] init];

testmodel *t1 = [[testmodel alloc] init];

t1.age = @"1";

testmodel *t2 = [[testmodel alloc] init];

t2.age = @"3";

testmodel *t3 = [[testmodel alloc] init];

t3.age = @"

19";

[marr addobject:t1];

[marr addobject:t2];

[marr addobject:t3];

// nspredicate *predicate = [nspredicate predicatewithformat:@"age = '19' || age = '3'"];

nspredicate *predicate = [nspredicate predicatewithformat:@"age like '19' || age like '3'

"]; nsarray *receivearr = [marr filteredarrayusingpredicate:predicate];

nslog(@"

%@",receivearr);

模型屬性為bool 或者 nsnumber

nsmutablearray *marr = [[nsmutablearray alloc] init];

person *p = [[person alloc] init];

p.age = @5;

p.isfemail = yes;

person *p2 = [[person alloc] init];

p2.age = @16;

p2.isfemail = no;

person *p3 = [[person alloc] init];

p3.isfemail = yes;

p3.age = @5;

[marr addobject:p];

[marr addobject:p2];

[marr addobject:p3];

nspredicate *predicate = [nspredicate predicatewithformat:@"age = 5

"]; //崩潰

nspredicate *predicate = [nspredicate predicatewithformat:@"age like 5"];

nspredicate *predicate = [nspredicate predicatewithformat:@"isfemail = yes"];

//崩潰

nspredicate *predicate = [nspredicate predicatewithformat:@"isfemail like yes"];

nsarray *array = [marr filteredarrayusingpredicate:predicate];

nslog(@"

%@",array);

二、求兩個陣列的交集

nsarray *array1 = [nsarray

arraywithobjects:

@1,@2,@3,@4,@5,@6,nil];

nsarray *array2 = [nsarray

arraywithobjects:

@4,@5,nil];

//self 指下面的array2

nspredicate *predicate = [nspredicate

predicatewithformat:@"self in %@",array1];

nsarray *resultarr = [array2 filteredarrayusingpredicate:predicate];

nslog(@"%@",resultarr);

三、過濾數值及字串 >,<,==,>=,<=,!=

//過濾數值

nsarray *array = @[@1,@2,@3,@4];

nspredicate *predicate = [nspredicate predicatewithformat:@"self >= 2"];

array = [array filteredarrayusingpredicate:predicate];

nslog(@"%@",array);

//過濾字串

nsarray *array = @[@"1",@"2",@"3",@"4"];

nspredicate *predicate = [nspredicate predicatewithformat:@"self >= '2'"];

array = [array filteredarrayusingpredicate:predicate];

nslog(@"%@",array);

三、範圍運算 in、between

nsarray *array = @[

@1,@2,@3,@4];

nspredicate *predicate = [nspredicate predicatewithformat:@"self between "];

array = [array filteredarrayusingpredicate:predicate];

nslog(@"

%@",array);

nsarray *array = @[@"1",@"2",@"3",@"4"];

nspredicate *predicate = [nspredicate predicatewithformat:@"self between "];

array = [array filteredarrayusingpredicate:predicate];

nslog(@"%@",array);

nsarray *array = @[@"1",@"2",@"3",@"4"];

nspredicate *predicate = [nspredicate predicatewithformat:@"self in "];

array = [array filteredarrayusingpredicate:predicate];

nslog(@"%@",array);

四、beginswith、endswith、contains

nsarray *array = @[@"1",@"21",@"31",@"4"];

//包含即可

nspredicate *predicate = [nspredicate predicatewithformat:@"self contains '1'"];

array = [array filteredarrayusingpredicate:predicate];

nslog(@"%@",array);

nsarray *array = @[@"1",@"21",@"31",@"4"];

//以1開頭

nspredicate *predicate = [nspredicate predicatewithformat:@"self beginswith '1'"];

array = [array filteredarrayusingpredicate:predicate];

nslog(@"%@",array);

nsarray *array = @[@"1",@"21",@"31",@"4"];

//以1結尾

nspredicate *predicate = [nspredicate predicatewithformat:@"self endswith '1'"];

array = [array filteredarrayusingpredicate:predicate];

nslog(@"%@",array);

五、萬用字元

nsarray *array = @[@"1",@"21",@"31",@"4"];

nspredicate *predicate = [nspredicate predicatewithformat:@"self like '??1'"];

array = [array filteredarrayusingpredicate:predicate];

nslog(@"%@",array);

nsarray *array = @[@"1",@"21",@"31",@"4"];

nspredicate *predicate = [nspredicate predicatewithformat:@"self like '1*'"];

array = [array filteredarrayusingpredicate:predicate];

nslog(@"%@",array);

mac開發 ios開發

但是,任何乙個作業系統上,只使用開發語言就去開發程式是不行的。還需要有介面庫。尤其是支援object c的介面庫。mac上使用oc開發應用程式,都會使用xcode這個ide,整合開發工具,xcode中整合了gui介面庫。可以直接拖動控制項到介面上。objective c是一門語言,而cocoa 是這...

IOS開發經驗

基本要點 首先,你的應用程式 不能導致手機故障 比如崩潰或螢幕問題 應用內的所有 資訊中不能用固定值代替可變變數 不要使用任何sdk裡面的私人api 不要使用任何sdk文件裡面沒有列出的功能 不要提及使用者裝置上不存在的硬體功能 如果需要網路連線,在沒有網路的情況下要告知使用者 不要 過度 模仿任何...

ios開發記事

1.loadview 和 viewdidload 區別 就是當view的nib檔案為nil時,手工建立檢視介面時呼叫loadview 當view的nib檔案存在的時候,初始化工作在viewdidload中實現 但是如果你的程式執行期間記憶體不足,檢視控制器接到didreceivememorywarn...