0

I am trying to compare strings and I'm not getting the logical result desired. I am getting the opposite. I would appreciate if anyone could point what I am doing wrong:

I have a data string that logs to console as "notfound"

if (![self.receivedDataString isEqual: @"notfound"]) {
     // IT SHOULD NOT BE EQUAL TO "notfound" BUT THIS IS GETTING CHOSEN
     NSLog(@"first option");
} else {
    //IT SHOULD EQUAL "notfound"
    NSLog(@"second option");
}

EDIT:

Here is receivedDataString

 self.receivedDataString= [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
user1904273
  • 4,562
  • 11
  • 45
  • 96
  • As written, "first option" will be logged if `self.receivedDataString` does *not* equal the string "notfound". What is your string and what result do you see? – rmaddy Oct 26 '15 at 03:46
  • 1
    Most likely your `self.receivedDataString` value has some non-obvious whitespace in it causing your issue. – rmaddy Oct 26 '15 at 03:48
  • the string is "notfound". Actually, I am always getting the first option and never the second option no matter what the value. The values in most cases are "notfound" but occasionally there is a hit, however, it always goes to first option. – user1904273 Oct 26 '15 at 03:55
  • 1
    As I said, you probably have some whitespace such as a newline at the end of your `self.receivedDataString` value. Check for that in the debugger. – rmaddy Oct 26 '15 at 03:56
  • could not find whitepace. – user1904273 Oct 26 '15 at 04:07
  • 2
    Can you try to do this: `NSLog(@"***%@***", self.receivedDataString)` and see what's the result? – manman Oct 26 '15 at 04:10
  • ran this again and got "found whitespace" Have to figure out how to remove it: NSRange whiteSpaceRange = [self.receivedDataString rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]]; if (whiteSpaceRange.location != NSNotFound) { NSLog(@"Found whitespace"); } – user1904273 Oct 26 '15 at 04:18
  • OK. I see what happened. It returns ****(null)****. Turns out receivedDataString was set inside an NSUrlConnection completion handler and when called outside of it is null. I guess I either have to move the test inside the handler or create a property to store the value in. Thanks! (I know I should be using NSSession) – user1904273 Oct 26 '15 at 04:24
  • This is not a duplicate of other question because the issue turned out not to be whitespace. You should check for whitespace. However, you should also check the value of your variable immediately before test. I was checking it a couple lines earlier but in a completion handler. The value did not carry over from the inflation handler. This became clear when I checked the value the line before the test (in the main method). – user1904273 Oct 26 '15 at 14:40

0 Answers0