当 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 方法中将 setext 重写为 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 出现过一次并且写得非常完美。 但我不明白为什么这段代码有效,而不是我的。
我的目标是检测搜索栏中的特殊字符。如果搜索栏中的文本包含此类字符,那么我可以在 NSComparisonResult
中禁用 NSDiactricInsensitiveSearch
。然后搜索结果将显示包含不属于变音字符的特殊字符的单词。
8 月 16 日编辑:
好的,这是我的 nscomparisonresult 代码。 @JohnBrighton:解决方案有效,但 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”。
最佳答案
您正在尝试打印“aa”,这是您不想要的,因为您刚刚声明了它。初始化它/打印其他东西,它就会工作。
编辑:这是检测 |string| 是否有效的好方法。包含“!”。
NSString *string = @"it works!";
NSRange range = [string rangeOfString:@"!"];
if (range.location != NSNotFound) {
/* This means the character has been found.
The position is at range.location. */
}
编辑#2:这是一种测量执行时间的方法。
#include <time.h>
...
clock_t start = clock(), end;
...[do your tasks]...
end = clock();
end = ((double)end - start) / CLOCKS_PER_SEC;
printf("Time taken: %f\n", end);
关于iPhone nscanner 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7081537/