【问题标题】:NSLinguisticTagger - enumerateLinguisticTagsInRange block not limiting rangeNSLinguisticTagger - enumerateLinguisticTagsInRange 块不限制范围
【发布时间】:2013-03-19 04:15:34
【问题描述】:

我在这样的方法中使用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

我做错了什么?

【问题讨论】:

    标签: ios objective-c xcode objective-c-blocks


    【解决方案1】:

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

    请务必注意,此方法将返回与给定范围相交的所有标记的范围。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      相关资源
      最近更新 更多