BOOL方法没有返回YES块内 [英] BOOL method not returning YES inside of block

查看:269
本文介绍了BOOL方法没有返回YES块内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了返回一个布尔值,如下图所示的新方法。

I have created a new method which returns a BOOL, shown below.

+(BOOL)checkIfGameAlreadyExistsAgainst:(PFUser *)opponentUser {
    // Find all the games where the current user is user1 and the opponentUser is user2
    PFQuery *currentUserIsUser1 = [PFQuery queryWithClassName:@"Game"];
    [currentUserIsUser1 whereKey:kMESGameUser1 equalTo:[PFUser currentUser]];
    [currentUserIsUser1 whereKey:kMESGameUser2 equalTo:opponentUser];
    [currentUserIsUser1 whereKey:kMESGameIsActive equalTo:[NSNumber numberWithBool:YES]];
    [currentUserIsUser1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects) {
            // We have games where the current user is user1
            // NEED TO RETURN NO TO THIS METHOD AND NOT RUN FURTHER IN METHOD...
            NSLog(@"Results returned for existing game where current user is User1. Results: %@",objects);
        } else {
            // If there are no objects from first query and no error we run the second query
            if (!error) {
                // Find all the games where the current user is user2 and the opponentUser is user1
                PFQuery *currentUserIsUser2 = [PFQuery queryWithClassName:@"Game"];
                [currentUserIsUser2 whereKey:kMESGameUser1 equalTo:opponentUser];
                [currentUserIsUser2 whereKey:kMESGameUser2 equalTo:[PFUser currentUser]];
                [currentUserIsUser2 whereKey:kMESGameIsActive equalTo:[NSNumber numberWithBool:YES]];
                [currentUserIsUser2 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                    if (objects) {
                        // We have games where the current user is user2
                        // NEED TO RETURN NO TO THIS METHOD AND NOT RUN FURTHER IN METHOD...
                        NSLog(@"Results returned for existing game where current user is User2. Results: %@",objects);
                    }
                }];
            }
        }
    }];


    return NO;
}

我的问题是如何在方法中一个块内返回YES的值。
请参见方法,说的部分//需要返回NO TO此方法并不会进一步运行方法...
我怎么能是回到这里。如果我想补充的回报是,我得到一个不兼容的指针类型的错误。

The problem I have is how to return a YES value within a block within the method. See the sections in the method which say // NEED TO RETURN NO TO THIS METHOD AND NOT RUN FURTHER IN METHOD... How can I return YES here. If I add return YES I get an incompatible pointer types error.

继此,一旦我有方法返回YES,我如何调用该方法,并做根据结果的东西。
比如我需要调用这个方法,如果这是真的话做别的事情,如果不做什么...

Further to this, once I have the method returning YES, how do I call this method and do something depending on the result. For example I need to call this method and if it is true then do something else, if not do nothing...

推荐答案

我不知道你问,所以这里是一个猜测:你希望你的块返回一个值 checkIfGameAlreadyExistsAgainst

I am not sure what you are asking, so here is a guess: you wish your block to return a value to checkIfGameAlreadyExistsAgainst.

当在构建块通常使的复制的从环境中引用的任何价值。如果你希望你的块的修改的在其环境中,您必须使用标记 __块可变变量。在您的code,这将是这个样子:

When a block in constructed it usually makes a copy of any value referenced from its environment. If you wish your block to modify a variable in its environment you must mark that variable with __block. In your code this would look something like:

__block BOOL blockStatus = YES;
[currentUserIsUser1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
 {
    ...
    blockStatus = NO;
 }
];

if (!blockStatus)
{
   ...
}

重要:您所呼叫的方法的名称, findObjectsInBackgroundWithBlock ,表明该块可能不会调用的同步,这意味着在执行块之前调用可能返回。如果是这样的话,你需要解决这个问题以不同的方式;这可能涉及调用的同步相当于 findObjectsInBackgroundWithBlock 或修改 checkIfGameAlreadyExistsAgainst ,这样它接受它与异步调用块的结果,而不是直接返回一个值。

Important: The name of the method you are calling, findObjectsInBackgroundWithBlock, suggests that the block may not be called synchronously, which means the call may return before the block is executed. If this is the case you need to tackle the issue in a different way; which may involve calling a synchronous equivalent of findObjectsInBackgroundWithBlock or modifying checkIfGameAlreadyExistsAgainst so that it accepts a block which it calls asynchronously with its result rather than returning a value directly.

心连心

这篇关于BOOL方法没有返回YES块内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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