【发布时间】:2013-07-02 23:09:39
【问题描述】:
我创建了一个返回 BOOL 的新方法,如下所示。
+(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 值。 请参阅方法中的部分 // NEED TO RETURN NO TO this METHOD AND NOT RUNFTHER IN METHOD... 我怎样才能在这里返回 YES。如果我添加 return YES,我会得到一个不兼容的指针类型错误。
除此之外,一旦我的方法返回 YES,我该如何调用该方法并根据结果执行某些操作。 例如我需要调用这个方法,如果它是真的然后做别的事情,如果不是什么都不做......
【问题讨论】:
-
我建议您使用“成功/失败”块作为方法参数,而不是返回 BOOL 值,尤其是当您认为方法中的代码将异步执行时。
标签: ios objective-c ios5 boolean objective-c-blocks