在pfcloud函数中返回布尔值 [英] Return bool inside pfcloud function

查看:97
本文介绍了在pfcloud函数中返回布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一种方法,用于检查用户是否有已经注册到该应用程序的朋友.但是,当我尝试从此块中返回时,出现编译器错误,提示:Incompatible block pointer types sending 'BOOL (^)(__strong id, NSError *__strong)' to parameter of type 'PFIdResultBlock' (aka 'void (^)(__strong id, NSError *__strong)')

I'm creating a method that checks if the user has any friends who are already registered to the app. However, when I'm trying to return from within this block, i get a compiler error saying: Incompatible block pointer types sending 'BOOL (^)(__strong id, NSError *__strong)' to parameter of type 'PFIdResultBlock' (aka 'void (^)(__strong id, NSError *__strong)')

这是我运行以下代码的时间:

This is when I run the code below:

 -(BOOL)hasFriend:(NSArray*)phoneNums{
    [PFCloud callFunctionInBackground:@"checkUsers"
                                   withParameters:@{@"array": phoneNums}
                                            block:^(id success, NSError *error) {
                                              if(success){
                                                return YES;
                                              }
                                              else{
                                                return NO;
                                                  }];
    }

我也尝试过:

-(BOOL)hasFriend:(NSArray*)phoneNums{
  __block bool hasFriend = nil;

  [PFCloud callFunctionInBackground:@"checkUsers"
                   withParameters:@{@"array": phoneNums}
                            block:^(id success, NSError *error) {
                              if(success){
                                hasFriend = YES;
                                NSLog(@"found a florin user!");
                              }
                              else{
                                hasFriend = NO;
                                }
                              }
                            }];

  NSParameterAssert(hasFriend);
  return hasFriend;
}

但是,这永远不会通过NSParameterAssert.

However, this never passes the NSParameterAssert.

推荐答案

直到函数返回后才执行该块,因此您不可能从函数中返回值...您将需要使用回调阻止(另一个)功能:

The block isn't executed until after the function returns so you can't possibly return a value from the function... You will need to use a callback block (another one) in your function:

-(void) hasFriend:(NSArray*)phoneNums withCallback:(void(^)(BOOL hasFriend))callback {

    [PFCloud callFunctionInBackground:@"checkUsers"
                       withParameters:@{@"array": phoneNums}
                                block:^(id success, NSError *error) {

                                    if (success)
                                        callback(YES);
                                    else
                                        callback(NO);

                                }];

}

要使用它,您需要提供这样的回调:

And to use it, you'll need to supply a callback like this:

[myObj hasFriend:nums withCallback:^(BOOL hasFriend) {

    if (hasFriend)
        NSLog(@"Found friend!");
    else
        NSLog(@"Friend not found...");

}];

这篇关于在pfcloud函数中返回布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆