gpt4 book ai didi

ios - filteredArrayUsingPredicate不过滤数据

转载 作者:行者123 更新时间:2023-12-01 19:02:00 25 4
gpt4 key购买 nike

我正在开发iOS应用,其中有searchBar。

我想要的是

当用户在searchBar中键入s时,,我想从我的数组中获取所有以字母Merchant_Name开头的s
这是我尝试过的代码段,但没有用,

我的arr包含:

 arr at index 0 = {
"Merchant_Id" = 1261;
"Merchant_Name" = "Magazine Mall";
"Merchant_Url" = "magazine-mall";
},
arr at index 1 = {
"Merchant_Id" = 1262;
"Merchant_Name" = MakeMyTrip;
"Merchant_Url" = makemytrip;
},
arr at index 2 = {
"Merchant_Id" = 1263;
"Merchant_Name" = Travelguru;
"Merchant_Url" = travelguru;
},
arr at index 3 = {
"Merchant_Id" = 1266;
"Merchant_Name" = Travelchacha;
"Merchant_Url" = travelchacha;
},
arr at index 4 = {
"Merchant_Id" = 1267;
"Merchant_Name" = Tradus;
"Merchant_Url" = tradus;
},
arr at index 5 = {
"Merchant_Id" = 1268;
"Merchant_Name" = ToyWorld;
"Merchant_Url" = toyworld;
},
arr at index 6 = {
"Merchant_Id" = 1269;
"Merchant_Name" = Topskin;
"Merchant_Url" = topskin;
},
....

码:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Merchant_Name LIKE[cd] %@ ",searchText];
NSMutableArray *filter1 =[[NSMutableArray alloc] init];
filter1 = [[arr filteredArrayUsingPredicate:predicate] mutableCopy];
NSLog(@"predicate = %@ ------- filter=%@",predicate,filter1);

日志显示:
predicate  = Merchant_Name LIKE[cd] "s" -------  filter=(
)

请帮助我,我在哪里做错了?

预先感谢。

最佳答案

NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name beginswith[c] %@", searchText];

NSMutableArray *filter1 =[[NSMutableArray alloc] init];
filter1 = [[arr filteredArrayUsingPredicate:predicate] mutableCopy];
NSLog(@"predicate = %@ ------- filter=%@",predicate,filter1);

了解更多

https://developer.apple.com/library/mac/documentation/cocoa/conceptual/predicates/Articles/pUsing.html

关于ios - filteredArrayUsingPredicate不过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22165887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com