如果语句不适用于下载的数据

时间:2011-12-22 22:13:06

标签: iphone objective-c if-statement

我编写了一些代码,通过PHP将数据发布到MySql数据库,PHP代码通过JSON返回'YES'或'NO'的值。然后我有一个if语句来检查它是YES还是NO。当我手动设置'working'字符串时if语句工作正常,但如果我使用JSON中的数据则不行。我已经检查过使用NSLog正确设置了,我真的看不出问题所在。这是我的代码的缩短版本:

-(void) dataDownloaded {
    NSDictionary *theDictionary = [_theArray objectAtIndex:0];
    NSString *worked = [theDictionary objectForKey:@"worked"];

    NSLog(@"%@", worked);

    if (worked == @"NO") {
        //code for if it didn't work
    } else if (worked == @"YES"){
        //code for if it did work
    } else {
        //code for if it doesn't return either value
    }
}

提前感谢任何能解决问题的人!

2 个答案:

答案 0 :(得分:2)

您的问题是您正在使用==进行比较。那不行。我想你需要阅读Objective-C - 试试http://www.cocoadevcentral.com/。以下是修复代码的方法:

-(void) dataDownloaded {
    NSDictionary *theDictionary = [_theArray objectAtIndex:0];
    NSString *worked = [theDictionary objectForKey:@"worked"];

    NSLog(@"%@", worked);

    if ([worked isEqualToString:@"NO"]) {
        //code for if it didn't work
    } else if ([worked isEqualToString:@"YES"]){
        //code for if it did work
    } else {
        //code for if it doesn't return either value
    }
}

答案 1 :(得分:1)

使用isEqualToString:进行字符串比较

if ([worked isEqualToString:@"NO"]) {

}