Domanda

I have a plist which contain urdu language Words and also have English language words.as below screenshot

enter image description here

Now my Code is to fetch data is given Below

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[words removeAllObjects];
[means removeAllObjects];
NSString *path = [[NSBundle mainBundle] pathForResource:
                  @"urdutoeng" ofType:@"plist"];
NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path];
 for (int i=0; i<[array2 count]; i++) {
    NSDictionary* dict =  [array2 objectAtIndex:i];
        [words addObject:[dict valueForKey:@"Urdu"]];
    [means addObject:[dict valueForKey:@"English"]];
    [Types addObject:[dict valueForKey:@"Nature"]];
    }
  }

This part of code work fine for me as my below screen shotenter image description hereenter image description here

Now problem is when i search any words through searchbar it return Empty result beacuse my search array contain words in Different Formate my code for search array is

listOfItems = [[NSMutableArray alloc] init];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:words forKey:@"Countries"];
[listOfItems addObject:countriesToLiveInDict];
copyListOfItems = [[NSMutableArray alloc] init];

My Searchbar code is

#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText
 {
[copyListOfItems removeAllObjects];
 NSLog(@"listOfItemsdata :%@",listOfItems);
for (NSString *cellLabel in [[listOfItems objectAtIndex:0] objectForKey:@"Countries"])
{
    NSComparisonResult result = [cellLabel compare:searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
    if (result == NSOrderedSame)
    {
        [copyListOfItems addObject:cellLabel];
    }
}
  }

#pragma mark UISearchDisplayController Delegate Methods
 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller  shouldReloadTableForSearchString:(NSString *)searchString
{
NSLog(@"searchstring :%@",searchString);
[self filterContentForSearchText:searchString];
return YES;
}

When i Nslog the listOfItems Array it show text some thing like

NSLog(@"listOfItemsdata :%@",listOfItems);

listOfItemsdata :( { Countries = ( " \U0627\U0628", " \U0627\U0628 \U0628\U06be\U06cc", " \U0627\U0628 \U062a\U0628", " \U0627\U0628 \U062a\U06a9", " \U0627\U0628 \U062c\U0628 \U06a9\U06c1", " \U0627\U0628 \U0633\U06d2", Its show that my data goes in some other formate thats why searchbar is unable to search it. Any help or Suggestion will be appriated.Thanks

È stato utile?

Soluzione

All the Urdu entries in the plist have a space as first character. This is the reason that searching always produced an empty list.

As a workaround, one can remove leading and trailing spaces from the entries before comparing:

for (NSString *cellLabel in [[listOfItems objectAtIndex:0] objectForKey:@"Countries"])
{
    NSString *trimmed = [cellLabel stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSComparisonResult result = [trimmed compare:searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
    if (result == NSOrderedSame)
    {
        [copyListOfItems addObject:cellLabel];
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top