ios比较两个nsstring

ios比较两个nsstring,ios,nsstring,udp,Ios,Nsstring,Udp,我有这个函数来检查来自udp连接的数据 - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSString *hello = @"hello"; if(response == hello){

我有这个函数来检查来自udp连接的数据

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag 
{    
 NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *hello = @"hello";
if(response == hello){
    [self debugPrint:[NSString stringWithFormat:@"ok"]];
}
else{
    [self debugPrint:[NSString stringWithFormat:@"Read:  \n%@",response]];
}

    [response release];

}
我发送了一个“hello”,但它从未返回“ok”消息,它跳转到else{}

有人能帮忙吗?谢谢你这行代码 if(response==hello) 应该是 if([响应IsequalString:hello])
因为“==”比较对象“respone”和对象“hello”的地址,为了进行比较,您必须使用isEqualToString函数:

NSString * str = @"oranges";
BOOL res = [str isEqualToString:@"apples"];
“isEqualToString:”是一种方法,它将指针(“aString”)指向NSString对象,并将其与调用它的NSString对象进行比较

if([thing1 IsequalString:thing2])

这里的“thing1”是指向NSString对象的指针,我们使用它的成员方法“isEqualToString:”将其与NSString对象“thing2”进行比较

所以“thing1”不是一个参数,而是一个对象,它有一个名为“isEqualToString:”的成员方法(或者更简单的函数):


谢谢你的回答,但是你的代码不起作用,我尝试了NSLOG(response),我在调试sreen中得到了hello,但是if每次都返回false。另外,如果我尝试类似于:NSString*hello=@“hello”;NSString*hello2=@“你好”;if([hello2isequaltostring:hello]){}if返回true。。我糊涂了!
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag 
{    
   NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  NSString *hello = @"hello";
// check like this
  if(response isEqualToString:hello){
    [self debugPrint:[NSString stringWithFormat:@"ok"]];
    }
  else{
    [self debugPrint:[NSString stringWithFormat:@"Read:  \n%@",response]];
    }

    [response release];

}