过滤字典和字符串数组的数组

时间:2022-09-25 08:11:05

I have one array which contains array of dictionary and string and I want to filter with dictionary value, but when I am doing filtering i am getting result only when I type first letter in text field i have written this code for nspredicate search also I have attached screenshot for NSarry containing values which I uses for filtering.

我有一个包含字典和字符串数组的数组,我想用字典值进行过滤,但是当我在进行过滤时,只有当我在文本字段中键入第一个字母时才会得到结果我已经为nspredicate搜索编写了此代码我也有NSarry的附加屏幕截图,包含我用于过滤的值。

Here is code for searching array:

这是搜索数组的代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text
{

    NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSLog(@"%@",searchStr);
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@ ",searchStr];
    NSArray *filteredArr = [arrmainData filteredArrayUsingPredicate:predicate];
    detailListArray=[[NSMutableArray alloc]initWithArray:filteredArr];
    [tblGlosary reloadData];

    return true;
}

Here is screensnap for array

这是数组的屏幕截图

过滤字典和字符串数组的数组

1 个解决方案

#1


0  

You have an array of mixed types (NSString and NSDictionary) which is generally a really bad idea (and won't be possible in Swift).

你有一个混合类型的数组(NSString和NSDictionary),这通常是一个非常糟糕的主意(并且在Swift中是不可能的)。

If you for some reason can't control this and really need to filter through it you need to check that you compare correctly depending on the type:

如果由于某种原因你无法控制它并且真的需要过滤它,你需要检查你是否正确比较取决于类型:

NSIndexSet *matches = [arrmainData indexesOfObjectsPassingTest:BOOL^(id obj, NSUInteger idx, BOOL *stop) {
    NSString *stringToCompare = nil;

    if ([obj isKindOfClass:[NSString class]]) {
        stringToCompare = (NSString *)obj;
    } else {
        NSDictionary *dict = (NSDictionary *)obj;
        stringToCompare = dict["Name"];
    }

    return [stringToCompare rangeOfString:searchString].location != NSNotFound;
 }];

 NSArray *filteredArray = [arrmainData objectsAtIndexes:matches];

But again, if you can, really reconsider mixing types in the same array, it's likely going to lead to problems down the road.

但同样,如果可以,真的重新考虑同一阵列中的混合类型,它可能会导致问题。

#1


0  

You have an array of mixed types (NSString and NSDictionary) which is generally a really bad idea (and won't be possible in Swift).

你有一个混合类型的数组(NSString和NSDictionary),这通常是一个非常糟糕的主意(并且在Swift中是不可能的)。

If you for some reason can't control this and really need to filter through it you need to check that you compare correctly depending on the type:

如果由于某种原因你无法控制它并且真的需要过滤它,你需要检查你是否正确比较取决于类型:

NSIndexSet *matches = [arrmainData indexesOfObjectsPassingTest:BOOL^(id obj, NSUInteger idx, BOOL *stop) {
    NSString *stringToCompare = nil;

    if ([obj isKindOfClass:[NSString class]]) {
        stringToCompare = (NSString *)obj;
    } else {
        NSDictionary *dict = (NSDictionary *)obj;
        stringToCompare = dict["Name"];
    }

    return [stringToCompare rangeOfString:searchString].location != NSNotFound;
 }];

 NSArray *filteredArray = [arrmainData objectsAtIndexes:matches];

But again, if you can, really reconsider mixing types in the same array, it's likely going to lead to problems down the road.

但同样,如果可以,真的重新考虑同一阵列中的混合类型,它可能会导致问题。