2013-03-26 81 views
0

我正在使用NSLinguisticTagger對象和他的方法enumerateTagsInRange:scheme:options:usingBlock。不能停止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; 
         } 
        } 
]; 

我希望是什麼,當我設置*停止= YES阻止停止他的循環。但是這沒有發生。該塊跳轉到另一個句子,但不停止。如果該字符串在句子結尾處有​​換行符(\ n),則按照我的預期(使用相同的代碼)停止該塊。但是當字符串有換行符時,該塊會導致內存泄漏。

任何人有任何想法發生了什麼,我可以嘗試阻止工作,我的預期?

串enumerateSubstringsInRange的方法:選擇:usingBlock:不解決我的實際解決我的問題。我想使用NSLinguisticTagger ...


我與蘋果開發者接觸技術支持與這個問題,他們導向我註冊這是一個錯誤。所以我做了一個錯誤報告,我在等待答案。任何新聞我將編輯這篇文章。

回答

0

我遵循iOS 7.1中的相同步驟,問題不再發生。

當我向蘋果公司報告說他們的問題可能已經在iOS7中得到解決。但是直到現在我纔有時間再次測試。

0

奇怪!

我能夠複製您的問題。如果將stop設置爲YES,則看起來像enumerateTagsInRange仍會通過任何剩餘句子的第一個單詞進行枚舉!解決該問題

的一種方法是將呼叫切換到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; 
     } 
    } 
}]; 

看到添加的if語句。

+0

我覺得這也很奇怪......這個解決方案解決了這個問題。但在我的代碼中,我爲相同的字符串多次調用此函數(僅適用於不同的範圍)。所以,如果我不停止該塊,每當我調用該函數時它就會到字符串的末尾,這會降低應用程序的速度。 – Yudmt 2013-03-28 17:04:18