iphone nscanner问题

时间:2011-08-16 16:27:36

标签: iphone nsscanner

当扫描一个动态输入的字符串时,似乎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/#documentation/Cocoa/Conceptual/Strings/Articles/Scanners.html

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。然后搜索结果将显示包含特殊字符的单词,这些字符不是diactric字符的一部分。


编辑16月8日:

好的,这是我的nscomparisonresult代码。 @JohnBrighton:`s解决方案有效,但nslog进入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];
        }
    }

编辑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];
        }
    }   
}

它提供了无数的NSLog,显示“时间:0”,而不是前一段代码中的“花时间:0.0000”。

1 个答案:

答案 0 :(得分:1)

你正在尝试打印出“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);