Ios 从数组中获取值
我的应用程序中有搜索栏 我想做的是,当用户在搜索栏中键入任何字符时,我想从数组中获取以相同字符开头的所有值 这是我的代码片段,我试过了,但它崩溃了Ios 从数组中获取值,ios,iphone,objective-c,ipad,nsarray,Ios,Iphone,Objective C,Ipad,Nsarray,我的应用程序中有搜索栏 我想做的是,当用户在搜索栏中键入任何字符时,我想从数组中获取以相同字符开头的所有值 这是我的代码片段,我试过了,但它崩溃了 for (int i=0; i<[arr count]; i++) { for(NSString *name in [[arr objectAtIndex:i] objectForKey:@"Merchant_Name"]) { NSRange r = [name rangeOfString:searchTex
for (int i=0; i<[arr count]; i++) {
for(NSString *name in [[arr objectAtIndex:i] objectForKey:@"Merchant_Name"])
{
NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the naames.
{
[arr addObject:name];
}
}
}
}
为此,应使用NSPredicate筛选阵列 编辑:使用谓词和过滤更容易阅读和理解。如注释中所述,它可能会慢一些,但您可能不会有10000000多个字符串要过滤,因此节省0.00001秒可能不值得再多写10行代码 这里有一个例子 筛选数组并获取以“A”或“c”开头的值的快速示例:
NSMutableArray *array =
[NSMutableArray arrayWithObjects:@"Nick", @"Ben", @"Adam", @"Melissa", nil];
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'a'"];
NSArray *beginWithB = [array filteredArrayUsingPredicate:bPredicate];
// beginWithB contains { @"Adam" }.
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'e'"];
[array filterUsingPredicate:sPredicate];
arr
中存在NSMutableDictionary
类型对象,请尝试以下操作以避免崩溃:
for(NSString *name in arr){
if(![name isKindOfClass:[NSString class]]){
continue;
}
NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the naames.
{
[Array addObject:name];
}
}
counter++;
}
使用下面的代码,您可以直接搜索
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Merchant_Name LIKE[cd] '%@' ",searchText];
NSArray *filter = [arr filteredArrayUsingPredicate:predicate];
使用下面的代码,您可以进行自定义搜索
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Merchant_Name beginswith[c] 'a'"];
NSArray *aNames = [arr filteredArrayUsingPredicate:predicate]
-使用此代码
-并将委托设置为Uitextfield
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *changedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF['Merchant_Name'] contains %@",changedText];
NSArray *filter = [arraydata filteredArrayUsingPredicate:predicate];
NSLog(@"%@%",changedText);
return YES;
}
什么是撞车?此外,这不是全部代码-什么是“计数器”,什么是数组?我们需要崩溃详细信息。但是您可能希望使用谓词而不是循环。是否
arr
中的所有元素都是NSString
,看起来像arr
中的元素是NSDictionary
。该错误意味着arr包含NSDictionary而不是NSStrings。我的arr包含此格式的值{“Merchant\u Id”=1012;“商户名称”=AajKiItem;“商户名称”=AajKiItem;},{“商户Id”=1013;“商户名称”=Aaneri;“商户Url”=Aaneri;},{“商户Id”=1015;“商户名称”=Adexmart;“商户Url”=Adexmart;},…
我想比较一下商户名称
我该如何比较?“NSPredicate…它比你能写的要快得多,优化得多。”-你有任何证明你的陈述的参考资料吗?根据我的经验,filteredarrayingpredicate
比显式循环慢,比较一下。它在这里崩溃NSArray*beginWithB=[arr filteredarrayingpredicate:bPredicate]代码>显示无法对非字符串的内容执行子字符串操作(lhs={“Merchant_Id”=1007;“Merchant_Name”=24HoursLoot;“Merchant_Url”=24HoursLoot;}rhs=a)
尝试了您的答案,并在此处显示崩溃NSArray*filter=[arr filteredarrayingpredicate:predicate]代码>我的日志显示,无法对非字符串的内容执行子字符串操作(lhs={“Merchant_Id”=1007;“Merchant_Name”=24HoursLoot;“Merchant_Url”=24HoursLoot;}rhs=a)
或者我尝试了你的第一个答案,我没有得到searchText
日志显示:Merchant_Name LIKE[cd]“%@”
我的数组过滤器为空,请帮我发送arr结果给我。请参阅问题“我只想要商户名称”中的我的编辑
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *changedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF['Merchant_Name'] contains %@",changedText];
NSArray *filter = [arraydata filteredArrayUsingPredicate:predicate];
NSLog(@"%@%",changedText);
return YES;
}