0

I have a question,I don't know why Method 2 can't get value ?

Method 1:can get value!

-(void) searchBar:(UISearchBar *) searchBar textDidChange:(NSString *)searchText{

    NSString *firstName = @"55566666666";

    AllMainEventRows3 = [AllMainEventRows mutableCopy];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",firstName];
    NSArray *filteredArray = [AllMainEventRows3 filteredArrayUsingPredicate:predicate];
    NSLog(@"filteredArray: %@", filteredArray);
}

LOG:

filteredArray: (
        {
        JoinSuccess = 55566666666;
        bmabout = "";
        bmaddr = "\U53f0\U5317\U5e02\U9752\U5cf6\U6771\U8def99\U865f10F-6";
})

Method 2:can't get value!

-(void) searchBar:(UISearchBar *) searchBar textDidChange:(NSString *)searchText{

    AllMainEventRows3 = [AllMainEventRows mutableCopy];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",searchText];
    NSArray *filteredArray = [AllMainEventRows3 filteredArrayUsingPredicate:predicate];
    NSLog(@"filteredArray: %@", filteredArray);
}

LOG:

filteredArray:(

)
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
SimonKira
  • 91
  • 1
  • 4
  • 13
  • While testing the second method, are you typing `55566666666` in the search bar? Did you also try to NSLog `searchText` object? – tolgamorf Mar 08 '13 at 09:34

1 Answers1

2

textDidChange Tells the delegate that the user changed the search text is delegate method of UISearchBarDelegate and it is called whenever text is changed within the search bar, I don't think the second method would get value because delegate methods are fired automatically means you are not calling them and you can not create multiple delegate methods with same name. you could declare some custom method for that purpose.

Problems in Predicate? would refer to usage of NSPredicate

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
  • I think he is using the same method in both cases with different implementations. He is not using both codes at the same time, yet he cannot implement the same method twice. – tolgamorf Mar 08 '13 at 09:29
  • I don't think the code will compile if he tries to implement the same method twice at the same time. – tolgamorf Mar 08 '13 at 09:36
  • sorry~ i use only one!not use method twice!i ask question not Said detailed!sorry!Now i have another question,how can use "predicate = [NSPredicate predicateWithFormat: @"self LIKE[cd] ,searchText"];",i use but Log empty! – SimonKira Mar 08 '13 at 09:36
  • try this `NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE[cd] %@", [NSString stringWithFormat:@"%@", searchText]];` – nsgulliver Mar 08 '13 at 09:40
  • Above code I must be type all string"55566666666" to search,finally can get the results,If i type "555" to search,finally get empty value!How can fix this problem!thx! – SimonKira Mar 08 '13 at 09:45
  • To nsgulliver: i use NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE[cd] %@", [NSString stringWithFormat:@"%@", searchText]]; my App Crash! – SimonKira Mar 08 '13 at 09:50
  • and what is crash saying in console? – nsgulliver Mar 08 '13 at 09:53
  • Above code I must be type all string"55566666666" to search,finally can get the results,If i type "555" to search,finally get empty value!How can fix this problem!thx! Above code is mean(the Top first code my write)! – SimonKira Mar 08 '13 at 09:53
  • To nsgulliver:crash break in NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE[cd] %@", [NSString stringWithFormat:@"%@", searchText]]; NSArray *filteredArray = [AllMainEventRows3 filteredArrayUsingPredicate:predicate]; log is (lldb) ! – SimonKira Mar 08 '13 at 09:57
  • @SimonKira http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.html#//apple_ref/doc/uid/TP40001793-CJBDBHCB – nsgulliver Mar 08 '13 at 10:03
  • Thanks nsgulliver! i will be try it! – SimonKira Mar 08 '13 at 10:16