iphone nscanner issue



似乎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显示了无穷多的

  • "scan: MainViewController"

  • "scan:(我输入的第一个字符)"

然后崩溃。MainViewController属于UIViewController类。

我从Apple文档中尝试了这些代码;

http://developer.apple.com/library/mac/文档/可可/概念/字符串/文章/Scanners.html:

NSString *string = @"Product: Acme Potato Peeler; Cost: 0.98 73n
    Product: Chef Pierre Pasta Fork; Cost: 0.75 19n
    Product: Chef Pierre Colander; Cost: 1.27 2n";
    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日编辑:

好,这是nscomparonresult的代码。@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];
        }
    }   
}

它出现了无数的nlog显示"Time taken: 0"而不是"Time taken: 0.0000"从你以前的代码

您正在尝试打印"aa",这是您不想要的,因为您刚刚声明了它。初始化它/打印一些其他的东西,它会工作。

编辑:这是一个很好的方法来检测是否|字符串|包含"!"。

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: %fn", end);

最新更新