0

I have some arrays whose contents are picked from db.I want to match up the text i have entered in 'searchTextField' with those array's contents which is taken from DB.

For example: my array contains, myArray={'bat man','bat and ball','ball'}; if i have entered 'bat' in 'searchTextField'; it must show index of matching text in array(in this case index 0 and 1).

How can i achieve this.. Waiting for your help..

1

1 Answer 1

4
NSMutableArray *tempArray = [NSMutableArray arrayWithObjects:@"bat man",@"bat and ball",@"ball", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'bat'"];
NSArray *result = [tempArray filteredArrayUsingPredicate:predicate];

result array will be containing the filtered objects, from there you can get the index as:

[tempArray indexOfObject:/the object from result array, one by one/]

contains[c] means search will be case insensitive. For more on predicates: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html

EDIT

set the textField's delegate as self. Before that go to YourFile.h, there add UITextFieldDelegate. now in textFieldShouldReturn do this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];

    NSMutableArray *tempArray = [NSMutableArray arrayWithObjects:@"bat man",@"bat and ball",@"ball", nil];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",textField.text];
    NSArray *result = [tempArray filteredArrayUsingPredicate:predicate];

    return YES;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.