It seems nsscanner cant do her job, when it scans a dynamically inputting string. Let me first write my codes:
NSString *setext = mySearchBar.text;
NSScanner *scanner = [NSScanner scannerWithString:setext];
NSString *a = @"a";
NSString *aa;
[scanner scanUpToString:a intoString:NULL];
while ([scanner isAtEnd] == NO)
{
if ( [scanner scanString:a intoString:NULL]);
{
NSLog (@" scan: %@ ", aa);
}
}
I have tried these codes in many of methods, but I get same result: the simulator terminated itself without any crash logs, when I write something in the searchbar. mySearchBar is added programmatically and is of UISearchBar class and is linked to self.
When I tried to rewrite setext to searchText in filterContent method for searching and comparing, NSLog shows endless amount of either
- "scan : MainViewController"
or
- "scan: (first character I entered)"
and then crashed. MainViewController is of UIViewController class.
I have tried these codes from Apple Documentation;
NSString *string = @"Product: Acme Potato Peeler; Cost: 0.98 73\n\
Product: Chef Pierre Pasta Fork; Cost: 0.75 19\n\
Product: Chef Pierre Colander; Cost: 1.27 2\n";
NSCharacterSet *semicolonSet;
NSScanner *theScanner;
NSString *PRODUCT = @"Product:";
NSString *COST = @"Cost:";
NSString *productName;
float productCost;
NSInteger productSold;
semicolonSet = [NSCharacterSet characterSetWithCharactersInString:@";"];
theScanner = [NSScanner scannerWithString:string];
while ([theScanner isAtEnd] == NO)
{
if ([theScanner scanString:PRODUCT intoString:NULL] &&
[theScanner scanUpToCharactersFromSet:semicolonSet
intoString:&productName] &&
[theScanner scanString:@";" intoString:NULL] &&
[theScanner scanString:COST intoString:NULL] &&
[theScanner scanFloat:&productCost] &&
[theScanner scanInteger:&productSold])
{
NSLog(@"Sales of %@: $%1.2f", productName, productCost * productSold);
}
}
and it works perfectly in any method. NSLog of this code showed up once and is perfectly written. But I cant see why this code works, not mine.
My goal is to detect special characters in the searchbar. If text in the searchbar contains such character(s), so I can disable NSDiactricInsensitiveSearch
in NSComparisonResult
. Then the search results will display words containing special characters which arent part of diactric characters.
EDIT 16 august:
Ok, here are my codes for nscomparisonresult. @JohnBrighton:`s solution works, but nslog came in a for loop and I couldnt click on anything on the app for the first character I enter... If I paste these codes before for loop, then searching doesnt work;
for (mystr in noWords) {
NSComparisonResult result;
NSString *string = mySearchBar.text;
NSRange range = [string rangeOfString:@"ø"];
if (range.location != NSNotFound) {
result = [mystr compare:searchText options:(NSCaseInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
NSLog (@" detected");
}
else
{
result = [mystr compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
NSLog(@"normal");
}
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:mystr];
}
}
EDIT 17 august, rewrited the codes above, so the whole method is visible:
- (void)filterContentForSearchText:(NSString*)searchText
{
for (mystr in noWords)
{
clock_t start = clock(), end;
NSComparisonResult result;
NSString *string = searchText;
NSRange range = [string rangeOfString:@"æ"];
NSRange range2 = [string rangeOfString:@"ø"];
NSRange range3 = [string rangeOfString:@"å"];
if (range.location != NSNotFound||range2.location != NSNotFound ||range3.location != NSNotFound ) {
// This means the character has been found.
// The position is at range.location.
result = [mystr compare:searchText options:(NSCaseInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
}
else {
result = [mystr compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
}
end = clock();
end = ((double)end - start) / CLOCKS_PER_SEC;
printf("Time taken: %f \n", (double)end);
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:mystr];
}
}
}
It came up with endless amount of NSLogs showing "Time taken: 0" instead of "Time taken: 0.0000" from your previous code.