简体   繁体   中英

How to return string from function after completion block executed?

I am calling languageConvertor function that returns string type value but i want to return that string type value after completion block executed please check the below code

-(NSString *)languageConvertor:(NSString *)str
{      

   [self.translator translateText:str completion:^(NSError *error, NSString *translated, NSString *sourceLanguage)
   {
         if (error)
         {
             [SVProgressHUD dismiss];
         }
         else
         {            
             NSString *fromLanguage = [[self currentLocale] displayNameForKey:NSLocaleIdentifier value:sourceLanguage];
             [SVProgressHUD dismiss];
         }
         }];
   return  translated;
}

Now in the above code i want to return translated string but after executed completion block so any one plz suggest me?

As you are using completion block for translating the text. That block is called asynchronous. That why the return statement is called before translating the text.

So to return translating text you need to add a block parameter in your method. As per shown below.

-(void)languageConvertor:(NSString *)str completionBlock:(void(^)(NSString *strText, NSError *error)) completion {
[self.translator translateText:str completion:^(NSError *error, NSString *translated, NSString *sourceLanguage) {
     if (error) {
         [SVProgressHUD dismiss];
     }
     else {
         NSString *fromLanguage = [[self currentLocale] displayNameForKey:NSLocaleIdentifier value:sourceLanguage];
         [SVProgressHUD dismiss];
     }

         if (completion) {
             completion(translated, error);
         }
 }];
}

Let me know if you have any query.

Thanks.

No, You can not return from the Block. You have to find a different way to do it.

Like

-(NSString *)languageConvertorForString:(NSString*)str withComletionBlock:(void (^)(NSString *translated, NSError *error))block

  {      

  [self.translator translateText:str completion:^(NSError *error, NSString *translated, NSString *sourceLanguage)

 {
      [SVProgressHUD dismiss];
      block(translated, error);

 }];

 }

Block takes your process asynchronous so its next line is executed just as you call that block function.

So you have to create your own block get the result of existing block.

- (void)languageConvertor:(NSString *)str withCompletion:(void (^)(NSString *string, NSError *error))completion

{

    [self.translator translateText:str completion:^(NSError *error, NSString *translated, NSString *sourceLanguage)

     {
         if (error)
         {
             completion(nil, error);
             [SVProgressHUD dismiss];

         }
         else
         {             NSString *fromLanguage = [[self currentLocale] displayNameForKey:NSLocaleIdentifier value:sourceLanguage];
             completion(yourReturnString, nil);
             [SVProgressHUD dismiss];
         }
     }];
}

And call the method like,

[yourObject languageConvertor:yourString withCompletion:^(NSString *string, NSError *error) {
        if(error) {

        } else {

        }
    }];

If you want to do some UI change on return then please call the completion block from main queue,

dispatch_async(dispatch_get_main_queue(), ^{
                completion(yourString, nil);
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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