I’m trying to get the calculator app I’ve built to trigger some simple code if it detects I pushed the decimal point button (to only allow one decimal point per value). However, for some reason, these if statements regarding digit aren’t being triggered. I’ve NSLogged the output of it and it is indeed showing as “.” so I can’t quite figure out why. I’ve changed the . to any other value too, nothing seems to trigger ’em.
NSString *digit = [sender currentTitle];
if (self.userIsInTheMiddleOfEnteringANumber) {
self.display.text = [[[self display] text] stringByAppendingString:digit]; // All in one line!
NSLog(@"Digit is equal to %@",digit);
if (digit == @".") NSLog(@"That was a decimal point!");
} else {
self.display.text = digit;
if (digit == @".") NSLog(@"You can't begin a number with a decimal!");
self.userIsInTheMiddleOfEnteringANumber = YES; // If they aren't typing, now they are!
}
Dont compare strings with ==. Use
isEqualToString:==checks for pointer equality, not string equality.