当它扫描动态输入的字符串时,nsscanner 似乎无法完成她的工作。让我先写我的代码:
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);
}
}
我已经在许多方法中尝试了这些代码,但我得到了相同的结果:当我在搜索栏中写一些东西时,模拟器在没有任何崩溃日志的情况下自行终止。mySearchBar 以编程方式添加,属于 UISearchBar 类,并链接到 self.
当我尝试在 filterContent 方法中将 settext 重写为 searchText 以进行搜索和比较时,NSLog 显示了无穷无尽的数量
- “扫描:MainViewController”
或者
- “扫描:(我输入的第一个字符)”
然后坠毁。MainViewController 属于 UIViewController 类。
我已经尝试过 Apple 文档中的这些代码;
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);
}
}
它可以在任何方法中完美运行。这段代码的 NSLog 出现过一次,并且写得很完美。但我不明白为什么这段代码有效,而不是我的。
我的目标是检测搜索栏中的特殊字符。如果搜索栏中的文本包含这样的字符,那么我可以禁用NSDiactricInsensitiveSearch
. NSComparisonResult
然后搜索结果将显示包含特殊字符的单词,这些字符不是变音字符的一部分。
编辑 8 月 16 日:
好的,这是我的 nscomparisonresult 代码。@JohnBrighton:`s 解决方案有效,但 nslog 进入了 for 循环,我无法在应用程序上单击我输入的第一个字符的任何内容...如果我在 for 循环之前粘贴这些代码,则搜索不起作用;
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];
}
}
编辑 8 月 17 日,重写了上面的代码,所以整个方法是可见的:
- (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];
}
}
}
它提出了无数的 NSLogs 显示“所用时间:0”而不是“所用时间:0.0000”来自您之前的代码。