0

I am having problems in comparing two string objects in 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:

@property(nonatomic,retain) NSString *detailFacility;

in my .m file in viewDidLoad function:

- (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:

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.

3
  • Is detailFacility an NSString? I don't see its declaration. Never mind. I just saw it. Commented Apr 2, 2012 at 16:37
  • Could this be an encoding issue. You're getting the Facility ID from a JSON object, might the encoding of that string be different than your app defined string even though they log the same? Commented Apr 2, 2012 at 16:43
  • repost of How to compare string objects in objective-c? Commented Apr 2, 2012 at 17:30

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.