after full research, I don't find a solution to my problem. I have a tableview wich contains object of Contact. This contact have name,mobile,street ...

My problem is :
when I make a search , I want to search about name of contact. For the moment I make a research about String value

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",searchText];


It s work perfectly , but now, I won't make my research with names of contacts. How can I proceed ?

When I put that :

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@",searchText];


I have an error :

erminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x8d76100> valueForUndefinedKey:]: <br/><br/>this class is not key value coding-compliant for the key name.' .



In my class Contact I have :

@property NSString * name;
@property NSString * display_name;
@property NSString * phone;
@property NSString * mobile;

Someone can tell step by step how make a research with an nsmutablearray that contains ojetcs ?

有帮助吗?

解决方案 3

What happens if you try:

 NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name == %@", searchText];
 NSArray *temp = [YOUR_ARRAY filteredArrayUsingPredicate:filterPredicate]; ?

EDIT

Try this:

NSMutableArray *searchResults = [[NSMutableArray alloc] init];

[YOUR_ARRAY enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    Contact *contact = obj;

    if ([[contact name] isEqualToString:searchText])
        [searchResults addObject:contact];
}];

//The search results array should now contain the matching items:
NSLog(@"Search Results:%@", searchResults);

其他提示

Try this one

 NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@ OR display_name contains[cd] OR phone contains[cd] OR mobile contains[cd]", searchText];

Using Predicates with Arrays

NSArray and NSMutableArray provide methods to filter array contents. NSArray provides filteredArrayUsingPredicate: which returns a new array containing objects in the receiver that match the specified predicate. NSMutableArray provides filterUsingPredicate: which evaluates the receiver’s content against the specified predicate and leaves only objects that match.

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];
// array now contains { @"Nick", @"Ben", @"Melissa" }

If you use the Core Data framework, the array methods provide an efficient means of filtering an existing array of objects without—as a fetch does—requiring a round trip to a persistent data store.

Here LINK for Details

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top