2013-03-19 99 views
0

我使用enumerateLinguisticTagsInRange的方法中,像这样:NSLinguisticTagger - enumerateLinguisticTagsInRange块不是限制范围

[nonAttributedString enumerateLinguisticTagsInRange:stringRange 
              scheme:NSLinguisticTagSchemeTokenType 
              options:NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerJoinNames 
             orthography:[NSOrthography orthographyWithDominantScript:@"Latn" languageMap:languageMap] 
             usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) { 

     // If the token is a word... 
     if ([tag isEqualToString:@"Word"]) 
     { 
      // (And add to the tracking dictionary) 
      NSMutableDictionary *wordAndRange = [[NSMutableDictionary alloc] init]; 
      [wordAndRange setObject:[NSNumber numberWithInt:(tokenRange.location + tokenRange.length)] forKey:[nonAttributedString substringWithRange:tokenRange]]; 

      [typedWordsAndRanges addObject:wordAndRange]; 
     } 
    }]; 

打标签工作正常,但它不是限制范围stringRange。它通过整个nonAttributedString进行枚举。

如果我包括以下日志权这一块上面:

NSLog(@"########### stringRange.location = %d", stringRange.location); 
NSLog(@"########### stringRange.length = %d", stringRange.length); 
NSLog(@"########### substring = %@", [nonAttributedString substringWithRange:stringRange]); 

我得到以下输出:

2013-03-18 21:06:27.744 WEJ[13231:c07] ########### stringRange.location = 10 
2013-03-18 21:06:27.745 WEJ[13231:c07] ########### stringRange.length = 4 
2013-03-18 21:06:27.805 WEJ[13231:c07] ########### substring = de 

所以stringRange是正确的。但是,它仍然通过整个nonAttributedString来列举。

我在做什么错?

回答

0

docs for enumerateTagsInRange:scheme:options:usingBlock:状态:

重要的是要注意,此方法将返回相交给定范围内的所有令牌的范围是很重要的。

如果你想只考虑那些stringRange完全令牌,你就必须调用enumerateTagsInRange::::之前,nonAttributedString检索子。