11

I have UISearchBar, UITableView, a web service which returns a NSMutableArray that contain objects like this:

//Food.h
Food : NSObject { 
    NSString *foodName;
    int idFood;
}

@property (nonatomic, strong) NSString *foodName;

And the array:

Food *food1 = [Food alloc]initWithName:@"samsar" andId:@"1"];
Food *food2 = [Food alloc] initWithName:@"rusaramar" andId:@"2"];

NSSarray *array = [NSArray arrayWithObjects:food1, food2, nil];

How do I filter my array with objects with name beginning with "sa"?

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
samir
  • 4,501
  • 6
  • 49
  • 76

2 Answers2

31

You can filter any array like you'd like to with the following code:

NSMutableArray *array = ...;

[array filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    return [evaluatedObject.foodName hasPrefix:searchBar.text];
}];

This will filter the array "in-place" and is only accessible on an NSMutableArray. If you'd like to get a new array that's been filtered for you, use the filteredArrayUsingPredicate: NSArray method.

Ash Furrow
  • 12,391
  • 3
  • 57
  • 92
  • Thanks for your answer, what's [evalutedObject objectName] ?? – samir Mar 04 '12 at 20:14
  • `objectName` was the property on the `NSObject` subclass in your question. If you want to filter based on a different property of the objects in your array, just use than property name instead. – Ash Furrow Mar 04 '12 at 20:16
  • I am sorry, i am doing like this : myObject *obj = [myObject new]; myArray = [myArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { return [[obj objectName] hasPrefix:searchTextField.text]; }]]; – samir Mar 04 '12 at 20:25
  • and it's not working, myArray is the array witch contani my objects – samir Mar 04 '12 at 20:26
  • What does the definition of `myObject` look like? In your question, it seems to have an `NSString` property (or instance variable) called `objectName`. Can you please expand your question with more of `myOjbect.h`? – Ash Furrow Mar 04 '12 at 20:29
  • OK so what you need to do is add a property called `foodName` in the header file and `@synthesize foodName` in the implementation file. I'll update my answer. – Ash Furrow Mar 04 '12 at 20:41
  • 2
    A block predicate seems like overkill for this case. You could just use `[NSPredicate predicateWithFormat:@"foodName beginswith %@", searchBar.text]`. – Chuck Mar 04 '12 at 20:59
  • Yes, exactly, the `evaluatedObject` is food1, then food2, etc. It's all of the objects in the array, one-by-one. – Ash Furrow Mar 04 '12 at 21:14
  • 1
    Tanks, it's working with : [NSPredicate predicateWithFormat:@"foodName beginswith[cd] %@", searchBar.text] – samir Mar 04 '12 at 21:21
2
NSString *predString = [NSString stringWithFormat:@"(foodName BEGINSWITH[cd] '%@')", @"sa"];

NSPredicate *pred = [NSPredicate predicateWithFormat:predString];

NSArray *array = [arr filteredArrayUsingPredicate:pred];
NSLog(@"%@", array);
Undo
  • 25,519
  • 37
  • 106
  • 129
abhi
  • 563
  • 6
  • 9