Search code examples
objective-ciosfloating-pointnsrange

NSRange not matching with NSNotFound


I have the following method:

- (IBAction)digitPressed:(UIButton *)sender {
    NSLog(@"%@", sender.currentTitle);
    if (self.userTypingNumber) {
        if (![sender.currentTitle isEqualToString:@"."]) 
            self.display.text = [self.display.text stringByAppendingString:sender.currentTitle]; 
        else {
            NSRange range = [sender.currentTitle rangeOfString:@"."];
            if (range.location == NSNotFound) { 
                self.display.text = [self.display.text stringByAppendingString:sender.currentTitle];
            }
        }
    } else {
        self.display.text = sender.currentTitle;
        self.userTypingNumber = YES;
    }
}

My problem is that my program never enters de NSNotFound if. I'm making a calculator and it should accept floating point numbers, but whenever I press the . it just passes right over the if. Any idea what could be wrong?


Solution

  • Looking at your logic, sender.currentTitle is always going to be @"." in your else statement, and so your range is always going to have a location of 0.