textfield text content comparision wont work

2019-09-07 04:10发布

问题:

I have this piece of code:

    if (email.text == emailComfirm.text) {
        UIAlertView *emailAllertView = [[UIAlertView alloc] initWithTitle:@"Succes" message:@"Yaaay, je kan nu gebuik gaan maken van Vogelspotter." delegate:self cancelButtonTitle:@"Direct aanmelden" otherButtonTitles: nil];
        [emailAllertView show];
        // Doing some other code here
        [self dismissModalViewControllerAnimated:YES];
    }else{
        NSLog(@"email: %@", email.text);
        NSLog(@"email: %@", emailComfirm.text);

        UIAlertView *emailAllertView = [[UIAlertView alloc] initWithTitle:@"Foutmelding" message:@"De opgegeven email adressen komen niet met alkaar overeen" delegate:self cancelButtonTitle:@"Probeer het nog eens" otherButtonTitles: nil];
        [emailAllertView show];
    }

somehow, no moather wath i type in the textfiels, the is statment always goes to the else, if i enter ee/ee or ee/ff. Can someone tell me what i'm doing wrong?

tnx in advance.

回答1:

You want to use [email.text isEqualToString:emailComfirm.text] to compare the contents of 2 NSString* objects. using == will compare the addresses of the 2 objects, not the contents of the objects. This is how pointers work How do pointer to pointers work in C?