从数组中获取值

时间:2014-02-27 07:56:19

标签: ios iphone objective-c ipad nsarray

我的应用中有searchBar,

我想要做的是当用户在searchBar中键入任何字符时,我想从我的数组中获取所有以相同字符开头的值。

这是我尝试过的代码片段,但它崩溃了..

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];

            }
        }

    }
}

显示错误: - [__ NSDictionaryM rangeOfString:options:]:无法识别的选择器发送到实例

我在哪里弄错了?

请提前帮助和致谢。

修改

arr =   {
        "Merchant_Id" = 1036;
        "Merchant_Name" = "Arzoo Hotels";
        "Merchant_Url" = "arzoo-hotels";
    },
        {
        "Merchant_Id" = 1037;
        "Merchant_Name" = "Ashika Mall";
        "Merchant_Url" = "ashika-mall";
    },
        {
        "Merchant_Id" = 1038;
        "Merchant_Name" = "At My Doorsteps";
        "Merchant_Url" = "at-my-doorsteps";
    },
        {
        "Merchant_Id" = 1039;
        "Merchant_Name" = "AVA Host";
        "Merchant_Url" = "ava-host";
    },

3 个答案:

答案 0 :(得分:1)

为此,您应该使用NSPredicate过滤您的数组。

编辑:使用谓词和过滤更具可读性和可理解性。正如评论中所述,它可能会更慢,但你可能不会有超过10,000,000个字符串需要过滤,因此保存0.00001秒可能不值得编写10行代码。

Documentation here

这是一个whole tutorial about search bar and filtering data

过滤数组并获取以“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];

答案 1 :(得分:1)

使用以下代码,您可以直接搜索

  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;
}

答案 2 :(得分:0)

NSMutableDictionary中有arr个类型对象,请尝试此操作以避免崩溃:

 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++;
}