Take the 2-minute tour ×

I don't understand this line of code: if ([@"+/-" isEqual:operation])

where operation is an NSString (and that operation can be either a "+" a "-" or a "/"). How is it possible to check for them all at once without getting an error?

Thanks, -z

share|improve this question

2 Answers 2

Never mind, I was missing a button that corresponds to that string, please ignore

share|improve this answer

It doesn't. isEqual compares a string to an object, and will return NO if the object is not a string. If you wish to compare strings use isEqualToString. But that will compare "+/-" to operation. To see if operation is one of +, -, or / you would have to check each character in turn, or use a regular expression, or something like this

NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"+-/"]];

if ([operatin rangeOfCharacterFromSet:set].location != NSNotFound) {
    NSLog(@"This string does not contain +-/ characters");
share|improve this answer
Dear svenito, Glad I asked after all. It turns out that my calculator button (and operation) was supposed to contain the string "+/-"; but I didn't know about NSCharacterSet, and will certainly be able to use this in the future. Thanks! -z –  zazu Nov 21 '10 at 0:57

Your Answer


