程序未进入循环(比较两个字符串值)

2024-09-30 16:24:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是在比较两个字符串的值。。我已经创建了一个Python脚本服务器,从中获取一个值,另一个值是本地字符串变量。我可以从服务器上检索正确的值,但是在比较时它不会进入循环。。在

这是我在客户机中比较值的代码

-(void)messageReceived:(NSString *)message  {

    [_messages addObject:message];
    NSLog(@"the received message is %@",message);

    if (message == @"1") 
        NSLog(@"the user is online");
    else if (message == @"0") {
        UIAlertView *alert;
        alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"The selected user is not online !" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles: nil];
        [alert show];
    }
}

这是一个日志,我可以看到正确的值

2012-02-25 21:44:35.376 iMessenger[2001:207]服务器称:1 2012-02-25 21:44:35.377 iMessenger[2001:207]收到的消息是1

有人能告诉我怎么回事吗。。谢谢


Tags: the字符串代码服务器脚本message客户机if
3条回答

您可以使用caseInsensitiveCompare

if([message caseInsensitiveCompare:@"1"])
NSLog(@"the user is online");

不要使用==来比较字符串。请改用[message isEqualToString:@"1"]。在

使用isEqualString比较NSString值:

if ([message isEqualToString:@"1"])
    NSLog(@"the user is online");
....

相关问题 更多 >