简体   繁体   English

无法停止NSLinguisticTagger的阻止

[英]Can't stop block of NSLinguisticTagger

I'm using NSLinguisticTagger object and his method enumerateTagsInRange:scheme:options:usingBlock. 我正在使用NSLinguisticTagger对象及其方法enumerateTagsInRange:scheme:options:usingBlock。

The problems is, when I'm using with a long string, without line breaks, I can't stop the block. 问题是,当我使用长字符串而没有换行符时,我无法停止该块。

Here's my code to initiate the string and NSLinguisticTagger: 这是我用来初始化字符串和NSLinguisticTagger的代码:

NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:@[NSLinguisticTagSchemeTokenType]
                                                                    options:NSLinguisticTaggerOmitOther | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerOmitWhitespace];

[tagger setString:@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."];

[tagger enumerateTagsInRange:NSMakeRange(0, [tagger.string length])
                      scheme:NSLinguisticTagSchemeTokenType
                     options:NSLinguisticTaggerOmitOther | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerOmitWhitespace
                  usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop){

                       NSString *word = [tagger.string substringWithRange:tokenRange];

                       if ([word compare:@"lamet" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
                            *stop = YES;
                       }
                  }
];

What I expected is, after I set *stop = YES the block stop his loop. 我期望的是,在我设置* stop = YES之后,该块停止了他的循环。 But this isn't happening. 但是,这没有发生。 The block jump to another sentence, but doesn't stop. 块跳到另一句话,但没有停止。 If the string has line breaks (\\n) in the end of the sentences, the block stop as I expected (with the same code). 如果字符串在句子的末尾有换行符(\\ n),则该块将按我的预期停止(使用相同的代码)。 But when string has line breaks, the block causes memory leaks. 但是,当字符串有换行符时,该块将导致内存泄漏。

Anyone have any idea what's happening and what I can try to block work has I expected? 任何人都知道发生了什么,我期望我可以尝试阻止哪些工作?

The method of string enumerateSubstringsInRange:options:usingBlock: does not resolve my problem in my actual solution... I'd like to use NSLinguisticTagger... 字符串enumerateSubstringsInRange:options:usingBlock:的方法无法在我的实际解决方案中解决我的问题...我想使用NSLinguisticTagger ...


I made a contact with Apple Developer Technical Support with this problem and they oriented me to register this as a bug. 我就此问题与Apple开发人员技术支持联系,他们让我将其注册为错误。 So I made a bug report and I'm waiting for answer. 所以我做了一个错误报告,我在等待答案。 Any news I'll edit this post. 任何新闻,我都会编辑这篇文章。

Strange! 奇怪!

I was able to duplicate your problem. 我能够复制您的问题。 If you set stop to YES, it looks like enumerateTagsInRange still enumerates through the first word of any remaining sentences! 如果将stop设置为YES,则enumerateTagsInRange仍会枚举剩余句子中的第一个单词!

One way around the problem would be to change the call to enumerateTagsInRange to: 解决该问题的一种方法是将对enumerateTagsInRange的调用更改为:

[tagger enumerateTagsInRange:NSMakeRange(0, [tagger.string length])
                      scheme:NSLinguisticTagSchemeTokenType
                     options:NSLinguisticTaggerOmitOther | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerOmitWhitespace
                  usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop){

    if (*stop == NO) { // Only deal with the word if stop if not set to YES
        NSString *word = [tagger.string substringWithRange:tokenRange];

        if ([word compare:@"lamet" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
            *stop = YES;
        }
    }
}];

See the added if statement. 请参阅添加的if语句。

I followed the same steps in iOS 7.1 and the problem does not occur anymore. 我在iOS 7.1中执行了相同的步骤,因此不再出现此问题。

When I reported that to Apple as a bug they answered that may have been solved in iOS7. 当我向Apple报告此错误时,他们回答说iOS7中可能已经解决了。 But only now I got time to test this again. 但是直到现在,我才有时间再次进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM