Reputation: 253
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.
Upvotes: 0
Views: 1012
Reputation: 162712
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).
Upvotes: 0
Reputation: 2175
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
Upvotes: 1