【问题标题】:How to convert an NSString into an int [duplicate]如何将 NSString 转换为 int [重复]
【发布时间】:2014-03-18 07:28:29
【问题描述】:

我想比较两个数字。

  1. 用户必须在 ViewController 中的 Textfield 中输入代码(此处为 1)。
  2. 输入的数字将相互比较,如果匹配,应用应切换到每个代码中定义的其他选项卡。

这就是我目前所得到的

int a,b,c,zz;
a=1;
b=2;
c=3;

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if (sender != self.done) 
      return;
    if (self.zahlencode.text.length > 0)
    {
       zz =self.zahlencode.text;
       if(zz == a)
       {
         zweiViewController *source = [segue sourceViewController];
       }
    }
}

【问题讨论】:

    标签: ios objective-c nsstring


    【解决方案1】:

    你可以在你的字符串上调用intValue

    NSString * intString = @"122";
    
    int myInt = [intString intValue];
    

    如果来自文本字段:

    int textFieldInt = [yourTextField.text intValue];
    

    您也可以将其用于其他类型的值,例如:

    CGFloat textFieldFloat = [yourTextField.text floatValue];
    double textFieldDouble = [yourTextField.text doubleValue];
    // etc ...
    

    【讨论】:

      【解决方案2】:

      这样使用

      int myInt = [someString integerValue];
      

      【讨论】:

        【解决方案3】:
        int a,b,c,zz;
        a=1;
        b=2;
        c=3;
        
        - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
        {
            if (sender != self.done) 
              return;
            if (self.zahlencode.text.length > 0)
            {
               zz =[self.zahlencode.text intValue];
               if(zz == a)
               {
                 zweiViewController *source = [segue sourceViewController];
               }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2012-08-22
          • 2010-11-25
          • 1970-01-01
          • 1970-01-01
          • 2014-10-19
          • 1970-01-01
          • 2015-04-30
          • 2013-01-16
          相关资源
          最近更新 更多