Swift代码错误:String类型的值没有成员Int

时间:2016-06-07 17:28:14

标签: swift xcode7

我正在尝试在Swift中构建一个简单的计算器,但我无法弄清楚我是如何或为什么得到错误(“String类型的值没有成员类型Int”)或如何解决它。到目前为止,这是我的代码:

$(document).ready(function(){  
  $('.activity-item p').each(function(index, element){
     var commentsHeight = $(this).height();
     alert(commentsHeight);

     //Refer to "this" element and search for the parents
     $(this).parents('li, div.activity-detail').css("height",commentsHeight);
  });
});

}

我哪里出错?

2 个答案:

答案 0 :(得分:4)

而不是:

firstNumber = calculatorDisplay.text?.Int()!

你想要这样的东西:

if let text = calculatorDisplay.text? {
    firstNumber = Int(text)!
}

或者如果你想生活在边缘:

firstNumber = Int(calculatorDisplay.text!)!

答案 1 :(得分:2)

Int()类型中没有String方法。

要将Int转换为String,请尝试以下操作:

guard let text = calculatorDisplay.text else {
    //the CalculatorDisplay has no text, deal with it accordingly
}
guard let firstNumber = Int(text) else {
    //the CalculatorDisplay has text, but it's not a valid Int
}

//use firstNumber