Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

objective c - IF clause is not validating NSString function return?

I have the function below that validates the return of another function (function searchPlaces that returns NSString). When I run it, it seems that the if clause is not being called, because the NSLog(@"Function Return: %@",nova url); appears in Console.

Can someone tell me if there's any mistake in the code? (For sure there is a mistake!)

- (void) buttonPushRandomViewController1 {

    UIViewController *randomViewController = [self randomViewController3];

    NSString *novaurl = [self searchPlaces];
    NSLog(@"Function Return: %@",novaurl);

    if (novaurl == @"OK") {
        randomViewController.title = @"Resultado";
        [self.master pushViewController:randomViewController animated:YES];

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:novaurl]];
        [webView loadRequest:request];
    }else if (novaurl == @"ZERO_RESULTS") {
        UIAlertView *zeroResults = [[UIAlertView alloc] initWithTitle: @"Ops!" message: @"Sorry, nothing found!" 
                                                             delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil];
        [zeroResults show];
        [zeroResults release];
    }
}

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should not comparing NSStrings with the == operator. The == operator performs reference equality on Objective-C object types: it only returns true if both operands refer to the exact same object. Even if two strings are copies of each other, == will return false if they refer to different instances.

You want to use value equality instead. You can compare strings using the -isEqualToString: method:

if ([novaurl isEqualToString:@"OK"]) {
    ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...