0

I am a beginner in Ios developement and I'm trying to compare two string who are normaly equal but comparaison fail everytime and go to the else:

NSString *response = [session.channel execute:@"gpio read 0" error:&error];//execute SSH command and write this one in log

NSLog(response);
if ([response isEqualToString:(@"1")]) {
    [session.channel execute:@"gpio write 0 0" error:&error];
} else {
    [session.channel execute:@"gpio write 0 1" error:&error];
}

here are my output, response is equal to 1 but the if is not used:

2014-04-08 20:01:38.065 domotiqueRPI[36948:1303] NMSSH: Exec command gpio read 0

2014-04-08 20:01:38.108 domotiqueRPI[36948:60b] 1

2014-04-08 20:01:38.109 domotiqueRPI[36948:1303] NMSSH: Exec command gpio write 0 1

Of course i search on stackOverFlow but all it tell to me : is to use isEqualToString method

Community
  • 1
  • 1

1 Answers1

3

You have a newline at the end of the response. Try this:

NSString *response = [session.channel execute:@"gpio read 0" error:&error];
response = [response stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

This will remove trailing and leading whitespaces/newlines from the response string.

BTW - no need for the parentheses around @"1".

rmaddy
  • 314,917
  • 42
  • 532
  • 579