Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 布尔方法的问题_Ios_Objective C_Parse Platform - Fatal编程技术网

Ios 布尔方法的问题

Ios 布尔方法的问题,ios,objective-c,parse-platform,Ios,Objective C,Parse Platform,我使用以下方法检查我的数据库中是否已存在电子邮件: -(BOOL) emailHasBeenTaken:(NSString *)email { PFQuery *query = [PFUser query]; [query whereKey:@"email" equalTo:email]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)

我使用以下方法检查我的数据库中是否已存在电子邮件:

-(BOOL) emailHasBeenTaken:(NSString *)email
{



        PFQuery *query = [PFUser query];
        [query whereKey:@"email" equalTo:email];

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error && objects.count>0) {
                NSLog(@"in the emailHasbeenTaken EMAIL IS DUPLICATE");
                [self duplicateEmail];

            } else
            {
                NSLog(@"in the emailHasBeenTaken, EMAIL IS NOT EXISTENT");
            }
        }];

    return YES;

}
我所面临的问题是,有时它会工作,有时它不会,我不确定我做错了什么,我所说的工作与否的意思是这。。。我检查新用户何时尝试使用以下代码注册:

     if (![self emailHasBeenTaken:self.emailTF.text]) {
    // EMAIL WAS NOT FOUND IN DATABASE SO IT WILL ASSIGN THE 
       TEXTFIELD VALUES TO USER PROPERTIES     

    email = self.emailTF.text;
                    user.email = email;
                } else
                {
                    [self duplicateEmail];
                    return;
                } 

我做错了什么?为什么我要添加
到我的if语句以获得所需的结果。。有没有一种方法可以从
BOOL
语句接收多个返回?i、 e.一个案例将返回YES,另一个案例将返回NO。

您总是返回
YES
首先,您正在进行一个异步调用来解析,该调用不起任何作用(除了调用
duplicateEmail
方法,您尚未发布该方法,因此我不知道该调用什么)然后您总是从您的
电子邮件hasbeentaken:
方法返回
YES

由于您需要进行异步调用以获取接收该电子邮件的任何用户,因此需要重新格式化方法的工作方式。您需要将其更改为以下内容:

- (void)emailHasBeenTaken:(NSString *)email completion:(void(^)(BOOL emailIsTaken, NSError *error))completionBlock
{
    void (^completionCopy)(BOOL, NSError *) = [completionBlock copy];

    PFQuery *query = [PFUser query];
    [query whereKey:@"email" equalTo:email];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        if (error) {
            NSLog(@"in the emailHasbeenTaken ERROR HAS OCCURRED");
            if (completionCopy) {
                completionCopy(NO, error);
            }
            return;
        }

        if (objects.count > 0) {
            NSLog(@"in the emailHasbeenTaken EMAIL IS DUPLICATE");
            if (completionCopy) {
                completionCopy(YES, nil);
            }
        }
        else {
            NSLog(@"in the emailHasBeenTaken, EMAIL IS NOT EXISTENT");
            if (completionCopy) {
                completionCopy(NO, nil);
            }
        }
    }];
}
NSString *emailFromTextField = self.emailTF.text;

[self emailHasBeenTaken:emailFromTextField completion:^(BOOL emailIsTaken, NSError *error) {
    if (error) {
        // TODO: handle any errors here
        return;
    }

    if (!emailIsTaken) {
        // Assuming "email" and "user" are instance variables here:
        email = emailFromTextField;
        user.email = email;
    }
    else {
        [self duplicateEmail];
    }
}];
请注意,此方法本身现在是异步的,因此在
if
语句中将其用作条件是不可能的。您可以这样使用它:

- (void)emailHasBeenTaken:(NSString *)email completion:(void(^)(BOOL emailIsTaken, NSError *error))completionBlock
{
    void (^completionCopy)(BOOL, NSError *) = [completionBlock copy];

    PFQuery *query = [PFUser query];
    [query whereKey:@"email" equalTo:email];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        if (error) {
            NSLog(@"in the emailHasbeenTaken ERROR HAS OCCURRED");
            if (completionCopy) {
                completionCopy(NO, error);
            }
            return;
        }

        if (objects.count > 0) {
            NSLog(@"in the emailHasbeenTaken EMAIL IS DUPLICATE");
            if (completionCopy) {
                completionCopy(YES, nil);
            }
        }
        else {
            NSLog(@"in the emailHasBeenTaken, EMAIL IS NOT EXISTENT");
            if (completionCopy) {
                completionCopy(NO, nil);
            }
        }
    }];
}
NSString *emailFromTextField = self.emailTF.text;

[self emailHasBeenTaken:emailFromTextField completion:^(BOOL emailIsTaken, NSError *error) {
    if (error) {
        // TODO: handle any errors here
        return;
    }

    if (!emailIsTaken) {
        // Assuming "email" and "user" are instance variables here:
        email = emailFromTextField;
        user.email = email;
    }
    else {
        [self duplicateEmail];
    }
}];

请你再详细解释一下,我的
duplicateEmail
方法所做的只是在我的视图中显示一个警报。当然,我会用我认为你应该做的事情更新我的答案。我尝试添加你的代码,但我得到一个生成错误“参数太多,无法阻止调用,预期为1,有2”@matt,我试过了,但是我一直得到一个构建失败的错误