0

Actually I want to populate an array to fill up my table view, but the problem is I have to compare two strings and if they match I wanted to add that string in the array. Here is my code:

for (NSDictionary *item in facilityZoneData) {
    NSString *zoneFacilityID = [NSString stringWithFormat:@"%@",[item objectForKey:@"FacilityId"]]; 

    if ([detailFacility isEqualToString:zoneFacilityID ]) {
        NSLog(@"object added");
    }
}

The detailFacility is NSString object which is declared in the header file. The problem is it is not at all comparing the string. I know that there are surely some values which are equal. tell me if I am missing something

5
  • 1
    What do you get if you add NSLog(@"#%@# #%@#", zoneFacilityID, detailFacility ); before your if() statement? Maybe there are some trailing spaces in one of the two? Commented Apr 2, 2012 at 15:46
  • 1
    Likely some difference in formatting -- blanks on the end, etc. Use NSLog thusly: NSLog(@"zoneFacilityId = >>>%@<<<, detailFacility = >>>%@<<<", zoneFacilityId, detailFacility); in order to detect any characters tacked on the ends. Commented Apr 2, 2012 at 15:47
  • Basically detailFacility is populated from another view controller and the it is showing up correct in NSLog, but I don't get why the comparison is not successful as there is surely matching string at some index Commented Apr 2, 2012 at 15:53
  • Have you tried running this in the debugger with a breakpoint set on the if statement? Commented Apr 2, 2012 at 15:55
  • Yes I have tried it with breakpoints but it seems like that it never executes the statements in the if-condition. Commented Apr 2, 2012 at 16:00

2 Answers 2

3

Your question is not clear. To compare strings you use

NSString isEqualToString: NSString

Like you did. Before your if-statement NSLog the detailFacility and zoneFacilityID objects to see what they contain. Cheers

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

Comments

-2

yea..i had the same problem a few days ago

i made a work-around. Do this:

if ([detailFacility UTF8String] == [zoneFacilityID UTF8String] ){ ...}

for some reason nsstring compassion is comparing addresses and pointers...try this and tell me if it works

i dont know why but that works for me :/

if it doesnt work..just use c function:

if(!strcmp([detailFacility UTF8String],[zoneFacilityID UTF8String])){
<are equal>
}else{
<aren't equal>
}

4 Comments

"I don't know why, but that works for me." is never the right answer; you are just pushing off a bug until later and, worse, building up a ton of code that assumes the broken code is behaving correctly.
ok..so after 10 min of brain stupidity on my part..i finally figured it out. I'm comparing c++ strings in my code ...not c strings , that's why it works ...so..my bad...but the c comparison works, i tested it
That doesn't make the C comparison code correct, though... converting to a UTF8String, then doing strcmp() is about as inefficient as possible....
well..it was an idea...i'm not saying its the best idea ever but it works.Its like this: you either wait a few hours to fix a problem like this or you use an inefficient way and come back later when you figured out what is wrong (or at least thats how i think)

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.