0

I have a little problem with my if statement when date.name is about this app News item : Does somebody can help me?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoreMenu *data = [self.fetchedResultsController objectAtIndexPath:indexPath];

    if ([data.name compare:@"about this app"]) {
        NSLog(@"About this app : %@", data.name);
    } else {
        NSLog(@"News item : %@", data.name);
    }
}
1
  • We can't help you with a problem if you won't tell us what it is. If you getting an error, then what kind? If you are getting unexpected results, then what do you expect?
    – dj18
    May 7, 2012 at 20:07

3 Answers 3

4

Similar to the pattern of the C standard function strcmp(), -compare: returns 0 in case of equality. Try

if ([data.name isEqualToString:@"about this app"]) {
    // code
}

or

if (![data.name compare:@"about this app"]) {
    // code
}

instead (the first notation is preferred).

1
  • +1 Better answer (although I think it's better to do == NSOrderedSame in the second case)
    – MByD
    May 7, 2012 at 16:16
4

Try:

if ([data.name isEqualToString:@"about this app"])
2
  • I know :), but it says i have to wait 7 min :(
    – Danny
    May 7, 2012 at 16:15
  • @DannyBoevéé - I think H2CO3 gave a better answer. consider accepting it (when you can)
    – MByD
    May 7, 2012 at 16:16
1

If you are checking for equality, you probably want to use:

isEqualToString: instead of compare:

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.