0

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

我究竟做错了什么?

4

1 回答 1

0

状态的文档enumerateTagsInRange:scheme:options:usingBlock:

需要注意的是,此方法将返回与给定范围相交的所有标记的范围。

如果您只想考虑那些完全在 中的标记,则stringRange必须nonAttributedString在调用之前检索该子字符串enumerateTagsInRange::::

于 2013-03-19T17:01:46.313 回答