嘿,伙计们,我正在试着做一个基本的计算器(波兰式的),但是我不明白为什么我停止双十进制输入的代码不能工作。我在另一个线程上找到了一种不同的方法,但我想知道为什么我的代码不能工作。我使用nsRange在标签中搜索已经输入的数字以查找小数,然后使用第二个条件来查看要追加的数字是否为小数请帮助..
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = [sender currentTitle]; // the number on the button pressed
NSString *decimalErrorChecker = [[self display] text]; //search line for .
NSRange range = [decimalErrorChecker rangeOfString:@"."];
if ((range.location != NSNotFound) && ((digit == @"."))) { //double if to stop 2nd
//SHOW ERROR
}else //APPEND DIGIT TO ALREADY ENTERED String.发布于 2012-02-23 03:24:43
使用isEqualToString而不是==来比较NSString
if ((range.location != NSNotFound) && [digit isEqualToString: @"."])发布于 2012-02-23 03:31:22
为什么不维护一个指示是否输入了小数的标志呢?
-(IBAction)digitPressed:(id)sender {
static BOOL didDecimal = NO;
// setup yadda
const BOOL digitIsDecimal = [digit isEqualToString:@"."]; // your use of == here is bad
if (digitIsDecimal && didDecimal) { error; return; }
didDecimal |= digitIsDecimal;
// append yadda
}当然,如果用户删除了小数,这也没有什么帮助,因此您可能需要在UITextViewDelegate中实现一些逻辑……
https://stackoverflow.com/questions/9401513
复制相似问题