stuart
stuart

Reputation: 455

filteredArrayUsingPredicate not working

I'm trying to use filteredArrayUsingPredicate with an array that has been built from data in a .plist file. Somehow it never seems to filter my array.

this is how my array is built:

    DrillDownAppAppDelegate *AppDelegate = (DrillDownAppAppDelegate *)[[UIApplication sharedApplication] delegate];
    self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];
    copyDataSource = [ tableDataSource mutableCopy];

and then my predicate goes like this,

NSString *searchFor = search.text;
[tableDataSource release];
tableDataSource = [copyDataSource mutableCopy];
if ([searchFor length] > 0) {
NSLog(@"array = %@",tableDataSource);
NSPredicate *pred = [NSPredicate predicateWithFormat:@"Self beginswith[c] %@",searchFor];
    [tableDataSource filteredArrayUsingPredicate:pred];
}

Upvotes: 1

Views: 2962

Answers (2)

Georg Fritzsche
Georg Fritzsche

Reputation: 99092

  • -[NSArray filteredArrayUsingPredicate:] returns a new array that contains the results
  • -[NSMutableArray filterUsingPredicate:] filters in-place.

So use the following instead:

[tableDataSource filterUsingPredicate:pred];

Upvotes: 6

James Bedford
James Bedford

Reputation: 28982

I think you need quotes round the string your subsituting into the predicateWithFormat: method. i.e:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"self beginswith[c] \"%@\"",searchFor];

Also try using the word "self" as all lower case, as in the keyword "self", and remember that instances of objects should really be started with a lowercase letter. For example, "appDelegate".

Upvotes: 1

Related Questions