user226372
user226372

Reputation: 165

Passing a variable out of block in completionHandler - __block is not helping me out

I think i need to modify this method to return the variable provided by the completionHandler - Any body know how do i do it the correct way?

dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"%@", [self getLanguageOfText:content]);
});



- (NSString *)getLanguageOfText:(NSString *)fewSentences {
__block NSString *returnLanguageString = @"Navi";

NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker];
[spellChecker setAutomaticallyIdentifiesLanguages:YES];
NSString *spellCheckText = fewSentences;

[spellChecker requestCheckingOfString:spellCheckText
                                range:(NSRange) {0, [spellCheckText length] }
                                types:NSTextCheckingTypeOrthography
                              options:nil
               inSpellDocumentWithTag:0
                    completionHandler: ^(NSInteger sequenceNumber, NSArray *results, NSOrthography *orthography, NSInteger wordCount) {
    //NSLog(@"dominant language = %@", orthography.dominantLanguage);
    //language = @"Hi";

    NSLog(@"dominant language = %@", orthography.dominantLanguage);

    returnLanguageString = orthography.dominantLanguage;
}];

return returnLanguageString;
}

But the returnLanguageString is always "Navi" but i want the orthography.dominantLanguage to be returned.

Please note : I need to have this method and the NSSpellChecker code

The few sentences i mentioned above will be like this and it is stored in NSString :

40
00:02:59.000 --> 00:03:01.000
彼らは私達にメッセージを送った

41
00:03:01.000 --> 00:03:03.000
彼らは彼らが望むものは何でも取ることができる

42
00:03:04.000 --> 00:03:05.000
我々は彼らにメッセージを送信します。

43
00:03:07.000 --> 00:03:09.000
これは私たちの土地である

Upvotes: 0

Views: 909

Answers (1)

Nicholas Hart
Nicholas Hart

Reputation: 1724

your block is running asynchronously. That means that it doesn't complete execution before your function returns--and the return value is unchanged.

You will need to handle setting this variable asynchronously. The easiest thing to do is use a block, eg:

- (void)getLanguageOfText:(NSString *)fewSentences {

    NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker];
    [spellChecker setAutomaticallyIdentifiesLanguages:YES];
    NSString *spellCheckText = fewSentences;

    [spellChecker requestCheckingOfString:spellCheckText
                                    range:(NSRange) {0, [spellCheckText length] }
                                    types:NSTextCheckingTypeOrthography
                                  options:nil
                   inSpellDocumentWithTag:0
                        completionHandler: ^(NSInteger sequenceNumber, NSArray *results, NSOrthography *orthography, NSInteger wordCount) {

        NSString * returnLanguageString = orthography.dominantLanguage;

        // handle the result somehow on the main queue
        dispatch_async(dispatch_get_main_queue() ^{
            [self doSomethingWithResult:returnLanguageString];
        });
    }];
}

Upvotes: 1

Related Questions