如何将NSString转换为int

时间:2014-01-10 14:43:37

标签: ios iphone objective-c math ios7

我一直在尝试制作一个计算器应用程序并且在使等号按钮工作时遇到了麻烦。我已经找到问题的根源,但是错了,但不知道如何解决它。

当用户输入数字时,按下乘法按钮(或+, - ,/),实例变量storedResult会在输入下一组数字之前保存该数字。当用户按下=时,应该发生的是当前屏幕(UILabel)乘以storedResult变量并将其转换回字符串并将其放入UILabel中。这不会发生。 UILabel总是读为零。

从控制台玩起来我发现问题似乎是当将当前屏幕(UILabel)转换为整数(或double,float,int)时,屏幕中的数字被设置为零。如果我要将其设置为添加,输出到UILabel的数字将是存储结果的数字。

以下是代码:

- (IBAction)equalsWhenPressed:(id)sender {

if ([operation  isEqual:@"x"]) {

    //Converting the UILabel to a string
    NSString *flabb = [NSString stringWithFormat:@"%@", self.screen.text];


    //converting the string to a integer
    NSInteger yahh = [flabb integerValue];

    //doing the multiplication
    NSInteger actualProblem = yahh * storedResult;

    NSString *result = [NSString stringWithFormat:@"%ld", (long)actualProblem];

    //Putting the result of the problem back in the UILabel        
    self.screen.text = result;


   operation = nil;


  }}

非常感谢! 在我的代码中修复'self.screen.text'之后我仍然是0,所以我上面改了它只是为了避免重复的答案。谢谢

这是viewController.m:

- (void)viewDidLoad
{

//NSLog(@"%@",self.screen);

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

  //self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Digits        Black Theme BG1"]];

 startedEnteringSecondNumber = @"NO";
 startedEnteringFirstNumber = @"NO";
}

- (IBAction)buttonOnePress:(id)sender {

if ([self.screen.text isEqual:@"0"]  ) {
    self.screen.text = @"1";

    startedEnteringSecondNumber = @"NO";

 } else if ([operation isEqual:@"x"]) {

    if ([startedEnteringSecondNumber isEqual:@"NO"]) {

        self.screen.text = @"1";

        startedEnteringSecondNumber = @"YES";
    } else {

        self.screen.text = [NSString stringWithFormat:@"%@1", self.screen.text];

    }

} else {

    self.screen.text = [NSString stringWithFormat:@"%@1", self.screen.text];
}
}



- (IBAction)buttonTwoPress:(id)sender {

if ([self.screen.text isEqual:@"0"]  ) {
    self.screen.text = @"2";

    startedEnteringSecondNumber = @"NO";

} else if ([operation isEqual:@"x"]) {

    if ([startedEnteringSecondNumber isEqual:@"NO"]) {

        self.screen.text = @"2";

        startedEnteringSecondNumber = @"YES";
    } else {

        self.screen.text = [NSString stringWithFormat:@"%@2", self.screen.text];

    }

} else {

    self.screen.text = [NSString stringWithFormat:@"%@2", self.screen.text];
}


}



- (IBAction)buttonThreePress:(id)sender {

if ([self.screen.text isEqual:@"0"]  ) {
    self.screen.text = @"3";

    startedEnteringSecondNumber = @"NO";

} else if ([operation isEqual:@"x"]) {

    if ([startedEnteringSecondNumber isEqual:@"NO"]) {

        self.screen.text = @"3";

        startedEnteringSecondNumber = @"YES";
    } else {

        self.screen.text = [NSString stringWithFormat:@"%@3", self.screen.text];

    }

} else {

    self.screen.text = [NSString stringWithFormat:@"%@3", self.screen.text];
  }
  }


 - (IBAction)buttonPressMultiply:(id)sender {



NSString *flabb = [NSString stringWithFormat:@"%@",[self.screen text]];

NSInteger lastInput = [flabb integerValue];

storedResult = lastInput;

operation = @"x";

}

- (IBAction)buttonPressDivide:(id)sender {


NSString *flabb = [NSString stringWithFormat:@"%@", [self.screen text]];

NSInteger actualDivider = [flabb integerValue];

NSInteger result = actualDivider / 4;

NSString *printedresult = [NSString stringWithFormat:@"%ld",(long)result];

self.screen.text = printedresult;
}





  - (IBAction)clearAllButton:(id)sender {


  self.screen.text = @"0";

  operation = nil;

   }

 - (IBAction)equalsWhenPressed:(id)sender {

  //NSLog([NSString stringWithFormat:@"b1a %ld", (long)storedResult]);

  // self.screen.text = [NSString stringWithFormat:@"%f", storedResult];

   //self.screen.text = [NSString stringWithFormat:@"%f", (long)[self.screen.text integerValue]       * storedResult];

// NSLog(@"%@", self.screen);


if ([operation  isEqual:@"x"]) {

     // NSLog([NSString stringWithFormat:@"%ld", (long)storedResult]);


    // NSLog(@"%@",operation);


    //Converting the UILabel to a string
    NSString *flabb = self.screen.text;

    flabb = [NSString stringWithFormat:@"%@", self.screen.text];


    //converting the string to a integer
    NSInteger yahh = [flabb integerValue];

    NSLog(@"%ld", (long)yahh);
    NSLog(@"%ld", (long)storedResult);
    NSLog(@"%@", self.screen.text);


    //doing the multiplication
    NSInteger actualProblem = yahh * storedResult;

    NSString *result = [NSString stringWithFormat:@"%ld", (long)actualProblem];


    //NSLog(@"%@", result);

    self.screen.text = result;


   //operation = nil;


  }}



  - (void)didReceiveMemoryWarning
  {
  [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
  }

 @end

5 个答案:

答案 0 :(得分:2)

NSString *flabb = [NSString stringWithFormat:@"%@", self.screen];

这行代码将返回一个非常糟糕的东西。您正在使用UILabel类型的对象,并出于某种原因将其转换为字符串。如果您想从UILabel获取文本,则需要使用:

NSString *flabb = [NSString stringWithFormat:@"%@", self.screen.text];

然后你可以在那个NSString上进行int转换,并希望得到你需要的东西。

答案 1 :(得分:2)

如果你这样做,你可能会有更好的运气:

NSString *flabb = self.screen.text;

而不是:

NSString *flabb = [NSString stringWithFormat:@"%@", self.screen];

答案 2 :(得分:1)

将UILAabel转换为String ???

如果我正确阅读并且自我。屏幕是您的标签,则可以使用self.screen.text

访问内部文字

修正乘法:

//doing the multiplication
NSInteger actualProblem = yahh + storedResult;
// multiply, not add : ------  *  ---------- ;

答案 3 :(得分:0)

//Converting the UILabel to a string
NSString *flabb = [NSString stringWithFormat:@"%@", self.screen];

这应该是:

//Converting the UILabel to a string
NSString *flabb = [NSString stringWithFormat:@"%@", self.screen.text];

您必须使用self.screen.text而不是self.screen。

答案 4 :(得分:0)

学习使用调试器,或至少使用日志语句。

这一行:

NSString *flabb = [NSString stringWithFormat:@"%@", self.screen];

有2个问题。首先,正如其他人所指出的,如果“self.screen”是一个标签(可怕的变量名称),那么你需要使用self.screen.text,而不是self.screen。

其次,使用stringWithFormat格式字符串为“%@”没有意义。这需要一个输入字符串,对它做了很多额外的工作,并返回完全相同的字符串的副本。不要那样做。这是浪费时间的无用之处。

该行应为

NSString *flabb = self.screen.text;

现在,让我们添加一些日志语句:

- (IBAction)equalsWhenPressed:(id)sender
{
  if ([operation  isEqual:@"x"])
  {

    //Get the UILabel's text (no conversion involved)
    NSString *flabb = self.screen.text;

    NSLog(@"IBOutlet self.screen = %@. label text = %@", self.screen, flabb);

    //converting the string to a integer
    NSInteger yahh = [flabb integerValue];

    NSLog(@"converted to an int, text = %d", yahh);


    //doing the multiplication
    NSInteger actualProblem = yahh + storedResult;

    NSLog(@"Integer result = %d", actualProblem);

    NSString *result = [NSString stringWithFormat:@"%ld", (long)actualProblem];

    NSLog(@"result = %@", result);

    //Putting the result of the problem back in the UILabel
    self.screen.text = result;

    operation = nil;
  }
}
相关问题