謂詞的使用 模糊搜尋

2021-06-26 14:50:58 字數 2203 閱讀 1419

main.m

#import "person.h"

int main(int argc, const char * argv)

",@"dfgh"];

predicate = [nspredicate predicatewithformat:s];

filterarrary = [persons filteredarrayusingpredicate:predicate];

nslog(@"filterarrary:%@",filterarrary);

//4.以...開頭 beginswith

s = [nsstring stringwithformat:@"name beginswith 'x'"];

predicate = [nspredicate predicatewithformat:s];

filterarrary = [persons filteredarrayusingpredicate:predicate];

nslog(@"filterarrary:%@",filterarrary);

//5.以...結尾endswith

s = [nsstring stringwithformat:@"name endswith 'u'"];

predicate = [nspredicate predicatewithformat:s];

filterarrary = [persons filteredarrayusingpredicate:predicate];

nslog(@"filterarrary:%@",filterarrary);

//模糊搜尋

//6.包含...字元

s = [nsstring stringwithformat:@"name contains 'a'"];

predicate = [nspredicate predicatewithformat:s];

filterarrary = [persons filteredarrayusingpredicate:predicate];

nslog(@"filterarrary:%@",filterarrary);

//7.以...結尾

// like *:任意多個字元 ?:表示乙個字元

/**n 以n結尾

*n* 字串中包含n,只要字串中有n就行

*n? 倒數第二位是n

?n* 第二位是n

*/s = [nsstring stringwithformat:@"name like '*n'"];

predicate = [nspredicate predicatewithformat:s];

filterarrary = [persons filteredarrayusingpredicate:predicate];

nslog(@"filterarrary:%@",filterarrary);

//刪選第二位是i的

s = [nsstring stringwithformat:@"name like '?i*'"];

predicate = [nspredicate predicatewithformat:s];

filterarrary = [persons filteredarrayusingpredicate:predicate];

nslog(@"filterarrary:%@",filterarrary);

}return 0;

}

person.h

@property (nonatomic, copy) nsstring *name;

@property (nonatomic, assign) nsinteger age;

+ (id)personwithname:(nsstring *)name withage:(nsinteger)age;

person.m

+ (id)personwithname:(nsstring *)name withage:(nsinteger)age 

- (nsstring *)description

- (void)dealloc

搜尋的一些謂詞

搜尋框的一些謂詞 nspredicate predicate nspredicate predicatewithformat username beginswith c searchstring 搜尋username為searchstring的物件 beginswith 查詢首字元符合條件的 end...

ios 謂詞的使用

剛入行的小女人,希望以後大家多多關照,把每天學到的知識記錄下,方便以後使用,有不對的地方希望大家多多指點,小女感激不盡。今天剛剛學了謂詞的使用,記錄下。返回乙個符合謂詞條件的陣列 nsarray newarray array filteredarrayusingpredicate predicate...

謂詞的使用 predicate

原文 一 運算子 1 between 比較運算子 nspredicate predicate nspredicatepredicatewithformat age 3 2 and 與 or 或 not 非 邏輯運算 nspredicate predic nspredicatepredicatewit...