简体   繁体   English

如何在Objective-C中比较字符串?

[英]How to compare strings in objective-c?

I am having problems in comparing two string objects in objective-c. 我在比较Objective-C中的两个字符串对象时遇到问题。 Here is my situation: 这是我的情况:

I have two NSString objects in my view controller as follow shown in my code below, in my .h file: 我的.h文件中的视图控制器中有两个NSString对象,如下代码所示:

@property(nonatomic,retain) NSString *detailFacility;

in my .m file in viewDidLoad function: 在我的.m文件中的viewDidLoad函数中:

- (void)viewDidLoad
{

NSData *facilityZoneURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"some URL..."]]];  
NSError *error;
NSDictionary *facilityZoneDict = [NSJSONSerialization JSONObjectWithData:facilityZoneURL options:kNilOptions error:&error];

NSArray *facilityZoneData = [facilityZoneDict objectForKey:@"Data"];

if (![facilityZoneData isKindOfClass:[NSArray class]]) {
    //JSON does not returned the Dictionary;
}

facilityZoneArray = [[NSMutableArray alloc] init];
NSLog(@"%@",detailFacility);
for (NSDictionary *item in facilityZoneData) {
    NSString *zoneFacilityID = [NSString stringWithFormat:@"%@",[item objectForKey:@"FacilityId"]]; 

    NSLog(@"Facility ID: %@ --- Zone ID: %@",detailFacility,zoneFacilityID);

    NSLog(@"%@",[zoneFacilityID isEqualToString:detailFacility]? @"YES" : @"NO");
    if ([zoneFacilityID isEqualToString:detailFacility]) {
        NSLog(@"object added");
    }
}

But the problem is it is not comparing the strings as it is surely matches as some position. 但是问题是它没有比较字符串,因为它肯定在某些位置上匹配。

here is my NSLOG situation: 这是我的NSLOG情况:

2012-04-02 12:12:42.998 CarbonIndex[11078:207] Facility ID: 1056 --- Zone ID: 1056
2012-04-02 12:12:42.999 CarbonIndex[11078:207] NO

As you can see that both the string are accurately matched, but the if-condition does not execute, Tell me what I am missing in it. 如您所见,两个字符串均已正确匹配,但是if条件没有执行,请告诉我其中缺少的内容。

It's likely an encoding issue. 这可能是编码问题。 Try using something like this and see if it helps: 尝试使用类似这样的东西,看看是否有帮助:

NSLog(@"%@",[zoneFacilityID compare:detailFacility]==NSOrderedSame? @"YES" : @"NO");

Compare works better with different encoding normalizations. 比较使用不同的编码规范化效果更好。 See details here: http://weblog.bignerdranch.com/?p=334 在此处查看详细信息: http : //weblog.bignerdranch.com/?p=334

More likely than not, one of your strings is not a string. 您的字符串之一不是字符串,很有可能。 It is an NSNumber and, thus, trying to do a string comparison is failing. 它是一个NSNumber,因此尝试进行字符串比较失败。

Try changing your logging to verify this: 尝试更改日志记录以验证这一点:

    NSLog(@"Facility ID: %@ --- Zone ID: %@",[detailFacility class],
              [zoneFacilityID class]);

If that is the case, then you'll likely want to change whatever code that is currently expected to store an NSString to storing an NSNumber, then use isEqual: on the number instances (as that will be faster and more straightforward than number->string conversions everywhere). 如果是这样,那么您可能想要将当前预期存储NSString的任何代码更改为存储NSNumber,然后在数字实例上使用isEqual:因为这比number->更快,更直接)字符串转换无处不在)。

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

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