Error when comparing NSString generated from NSUTF8StringEncoding

52 Views Asked by At

I use my app to receive data by using this method:

-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address withFilterContext:(id)filterContext {
  NSLog(@"didReceiveData");
  NSString* input = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  NSLog(@"%@", input);
  if ([input isEqual:@"LEDCube"]) {
        //do something
    }

Even though I send "LEDCube", the if statement always returns 0, that makes me unable to go further.

From NSLog, the content of input is "LEDCube". Therefore, I suspect that the problem is at the NSUTF8StringEncoding. How to deal with this problem?

Thank you in advance

1

There are 1 best solutions below

1
S Patel On

use isEqualToString instead of isEqual

if ([input isEqualToString:@"LEDCube"]) {
    //do something
}