开发者

If statement checking for NSString equality using ==

开发者 https://www.devze.com 2023-02-10 11:55 出处:网络
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.

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

精彩评论

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