1

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";

if (returnStringResults == yesText) {
    testLabel.text = @"Success";
    // DO ACTION HERE
}
jscs
  • 63,694
  • 13
  • 151
  • 195
iosfreak
  • 5,228
  • 11
  • 59
  • 102

2 Answers2

8
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
}
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
W Dyson
  • 4,604
  • 4
  • 40
  • 68
2

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.

datenwolf
  • 159,371
  • 13
  • 185
  • 298