简体   繁体   English

isEqualToString始终返回False

[英]isEqualToString always returns False

A little background here before I get started, basically we are looking to compare a UDP response with a string stored in Parse's database for our app. 在我开始之前有一点背景知识,基本上我们希望将UDP响应与存储在Parse数据库中的字符串进行比较。 This issue is that I can't seem to get the strings to be considered equal by the isEqualToString function. 这个问题是我似乎无法通过isEqualToString函数让字符串被认为是相等的。 Here's the code I have running now, I have tried a few work-arounds I've seen in other questions but it still doesn't work. 这是我现在运行的代码,我尝试了一些我在其他问题中看到的解决方法,但它仍然无效。

- (BOOL) onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
    if(tag == TAG_SINGLE_GRILL)
    {
        NSString *grillId = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        if(grillId.length > 11)
        {
            grillId = [grillId substringToIndex:11];
        }
        grillId = [NSString stringWithFormat:@"%@", grillId];
        if([grillId hasPrefix:@"GMG"])
        {
            for(int i = 0; i < [parseGrills count]; i++)
            {
                NSString *parseGrillId = [[parseGrills objectAtIndex:i] grillId];
                parseGrillId = [NSString stringWithFormat:@"%@", parseGrillId];
                //If we match the id, add it to found grills
                if([grillId isEqualToString:parseGrillId])
                {
                    //do stuff
                }
            }
        }
        NSLog(@"Grill ID : %@", grillId);
    }
    return TRUE;
}

parseGrills is an NSMutableArray with a very basic Grill object, I use synthesize for the properties, otherwise the .m file is essentially empty. parseGrills是一个带有非常基本的Grill对象的NSMutableArray,我使用synthesize作为属性,否则.m文件基本上是空的。

#import <Foundation/Foundation.h>

@interface Grill : NSObject

@property (nonatomic) NSString* grillId;
@property (nonatomic) NSString* ipAddress;

@end

Here's a screen shot of the debugger after it returns false 这是调试器返回false后的屏幕截图

调试器SS Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

I guess that they are of different encoding. 我猜它们的编码不同。

I have run this experiment and see that if the encoding is different, it will return NO . 我已经运行了这个实验,看到如果编码不同,它将返回NO So, try converting parseGrillId to utf8 with the code below. 因此,尝试使用下面的代码将parseGrillId转换为utf8

NSString *s1 = [NSString stringWithCString:"HELLO123" encoding:NSUTF8StringEncoding];
NSString *s2 = [NSString stringWithCString:"HELLO123" encoding:NSUTF16StringEncoding];
NSString *s3 = [NSString stringWithUTF8String:s2.UTF8String];

if ([s1 isEqualToString:s2]) {
    NSLog(@"s1 == s2");
}

if ([s1 isEqualToString:s3]) {
    NSLog(@"s1 == s3");
}

Will print s1 == s3 . 将打印s1 == s3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM