我正在开发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=(
)
请帮助我,我在哪里做错了?
提前致谢。
答案 0 :(得分:2)
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name beginswith[c] %@", searchText];
NSMutableArray *filter1 =[[NSMutableArray alloc] init];
filter1 = [[arr filteredArrayUsingPredicate:predicate] mutableCopy];
NSLog(@"predicate = %@ ------- filter=%@",predicate,filter1);
更多了解遵循此
答案 1 :(得分:1)
为BeginsWith更改你的LIKE
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Merchant_Name beginswith[c] %@ ",searchText];
NSMutableArray *filter1 =[[NSMutableArray alloc] init];
filter1 = [[arr filteredArrayUsingPredicate:predicate] mutableCopy];
NSLog(@"predicate = %@ ------- filter=%@",predicate,filter1);
答案 2 :(得分:0)
[NSPredicate predicateWithFormat:@"Merchant_Name beginswith %@", [searchText uppercaseString]];