开发者

If statement checking for NSString equality using ==

I am making a server call in some Objective-c code. If it returns as a @"yes", it will do an action. For some reason, the // DO ACTION HERE part is never reached.

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSString *returnStringResults = returnString;
NSString *yesText = @"yes";

i开发者_如何学Pythonf (returnStringResults == yesText) {
    testLabel.text = @"Success";
    // DO ACTION HERE
}


if ([returnStringResults isEqualToString:yesText]) {
    testLabel.text = @"Success";
    // DO ACTION HERE
}

Edit: As bbum pointed out, NSString *returnStringResults = returnString; does nothing.

So really, remove that line and use

if ([returnString isEqualToString:yesText]) {
    testLabel.text = @"Success";
    // DO ACTION HERE
}


You're comparing pointer addresses. The way this code works yesText and returnStringResults are pointers to different NSString instances, thus the pointers are not equal. You've to use NSString isEqualToString method to compare.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜