ios 謂詞的使用

2021-06-22 07:23:27 字數 2262 閱讀 9992

剛入行的小女人,希望以後大家多多關照,把每天學到的知識記錄下,方便以後使用,有不對的地方希望大家多多指點,小女感激不盡。

今天剛剛學了謂詞的使用,記錄下。。。

//返回乙個符合謂詞條件的陣列

nsarray *newarray = [ array filteredarrayusingpredicate:predicate];

for (person *person in newarray)

nspredicate *predicate = [nspredicate predicatewithformat:@" age <= 28"];

//表示指定物件是否滿足謂詞條件

for (person *person in array)

}

//格式佔位符

nspredicate *pre = [nspredicate predicatewithformat:@"age <= %d",30];

nsarray *array2 = [array filteredarrayusingpredicate:pre];

for (person *person in array2)

//邏輯運算子

//邏輯符號的加入,謂詞區分大小 && and || or

nspredicate *pre3 = [nspredicate predicatewithformat:@"name > 'bruse' && age < %d ",30];

nsarray *array3 = [array filteredarrayusingpredicate:pre3];

//關鍵字 注意字串一定要新增 ''

nspredicate *pre4 = [nspredicate predicatewithformat:@"self.name in "];

nsarray *array4 = [array filteredarrayusingpredicate:pre4];

for (person *person in array4)

//以**開始 --------beginswith

//beginswith 檢查某個字是否以**開頭

nspredicate *pre5 = [nspredicate predicatewithformat:@"self.name beginswith 'j' " ];

nsarray *array5 = [array filteredarrayusingpredicate:pre5];

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

//以**結束 --------endswith

//endswith 檢查某個字是否以**結尾

nspredicate *pre6 = [nspredicate predicatewithformat:@"self.name endswith 'e' " ];

nsarray *array6 = [array filteredarrayusingpredicate:pre6];

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

//包含 --------contains

//contains 檢查包含某個字元

nspredicate *pre7 = [nspredicate predicatewithformat:@"self.name contains '小' " ];

nsarray *array7 = [array filteredarrayusingpredicate:pre7];

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

//模糊查詢 --------like

nspredicate *pre8 = [nspredicate predicatewithformat:@"name like '*%@*' ",@"a"];

nsarray *array8 = [array filteredarrayusingpredicate:pre8];

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

iOS謂詞的簡單使用

謂詞 條件篩選 nsstring adc 123 判斷字串是不是以xx開頭 yes是 bool result adc hasprefix 1 判斷字串是不是以xx結尾 adc hassuffix 3 nslog d result 謂詞 用法接近於sqlite都是通過對應的語句來進行篩選和查詢 建立謂...

iOS中謂詞的使用

cocoa提供了乙個類nspredicate類,該類主要用於指定過濾器的條件,該物件可以準確的描述所需條件,對每個物件通過謂詞進行篩選,判斷是否與條件相匹配。謂詞表示計算真值或假值的函式。在cocoa中封裝的乙個資料庫框架cocoadata裡面 在進行查詢 包括模糊查詢 時同樣會要用到謂詞 下面對謂...

iOS中的謂詞NSPredicate

nspredicate的坑,正規表示式無論是否新增 或 系統會自動新增開始字元和結束字元,和我們平常使用的不太一樣,所以要寫出完整的匹配表示式。nsstring regex u4e00 u9fa5a za z nspredicate predicate nspredicate predicatewi...