IF子句不验证NSString函数返回? [英] IF clause is not validating NSString function return?

查看:79
本文介绍了IF子句不验证NSString函数返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的函数验证另一个函数的返回(函数searchPlaces返回NSString)。当我运行它时,似乎没有调用if子句,因为NSLog(@函数返回:%@,nova url);出现在控制台中。

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];
    }
}

提前致谢!

推荐答案

你不应该将 NSString == 运营商。 == 运算符对Objective-C对象类型执行引用相等:如果两个操作数都引用完全相同的对象,则它只返回true。即使两个字符串是彼此的副本, == 如果引用不同的实例,也会返回false。

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.

您想要使用值相等。您可以使用比较字符串 -isEqualToString: 方法:

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

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

这篇关于IF子句不验证NSString函数返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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