该应用程序基本上通过输入初始和最终速度和时间来计算加速度,然后使用一个公式来计算加速度。但是,由于文本框中的值是字符串,我无法将它们转换为整数。

@IBOutlet var txtBox1 : UITextField
@IBOutlet var txtBox2 : UITextField
@IBOutlet var txtBox3 : UITextField
@IBOutlet var lblAnswer : UILabel


@IBAction func btn1(sender : AnyObject) {

    let answer1 = "The acceleration is"
    var answer2 = txtBox1
    var answer3 = txtBox2
    var answer4 = txtBox3

当前回答

至于swift 3,我必须强制我的#%@!字符串和int加上一个“!”否则它就不起作用。

例如:

let prefs = UserDefaults.standard
var counter: String!
counter = prefs.string(forKey:"counter")
print("counter: \(counter!)")


var counterInt = Int(counter!)
counterInt = counterInt! + 1
print("counterInt: \(counterInt!)")

OUTPUT:
counter: 1
counterInt: 2

其他回答

斯威夫特4.0

let stringNumber = "123"
let number = Int(stringNumber) //here number is of type "Int?"


//using Forced Unwrapping

if number != nil {         
 //string is converted to Int
}

你也可以使用可选绑定而不是强制绑定。

eg:

  if let number = Int(stringNumber) { 
   // number is of type Int 
  }

你可以使用NSNumberFormatter(). numberfromstring (yourNumberString)。这很好,因为它返回一个可选的,然后你可以用if let测试,以确定转换是否成功。 如。

var myString = "\(10)"
if let myNumber = NSNumberFormatter().numberFromString(myString) {
    var myInt = myNumber.integerValue
    // do what you need to do with myInt
} else {
    // what ever error code you need to write
}

斯威夫特5

var myString = "\(10)"
if let myNumber = NumberFormatter().number(from: myString) {
    var myInt = myNumber.intValue
    // do what you need to do with myInt
  } else {
    // what ever error code you need to write
  }

我已经做了一个简单的程序,其中有2个TXT字段,您从用户输入并添加它们,以使其更容易理解,请找到下面的代码。

@IBOutlet weak var result: UILabel!
@IBOutlet weak var one: UITextField!
@IBOutlet weak var two: UITextField!

@IBAction func add(sender: AnyObject) {        
    let count = Int(one.text!)
    let cal = Int(two.text!)
    let sum = count! + cal!
    result.text = "Sum is \(sum)"
}

希望这能有所帮助。

@IBAction func calculateAclr(_ sender: Any) {
    if let addition = addition(arrayString: [txtBox1.text, txtBox2.text, txtBox3.text]) {
      print("Answer = \(addition)")
      lblAnswer.text = "\(addition)"
    }
}

func addition(arrayString: [Any?]) -> Int? {

    var answer:Int?
    for arrayElement in arrayString {
        if let stringValue = arrayElement, let intValue = Int(stringValue)  {
            answer = (answer ?? 0) + intValue
        }
    }

    return answer
}

用这个:

// get the values from text boxes
    let a:Double = firstText.text.bridgeToObjectiveC().doubleValue
    let b:Double = secondText.text.bridgeToObjectiveC().doubleValue

//  we checking against 0.0, because above function return 0.0 if it gets failed to convert
    if (a != 0.0) && (b != 0.0) {
        var ans = a + b
        answerLabel.text = "Answer is \(ans)"
    } else {
        answerLabel.text = "Input values are not numberic"
    }

OR

使你的UITextField KeyboardType为DecimalTab从你的XIB或故事板,并删除任何if条件做任何计算,即。

var ans = a + b
answerLabel.text = "Answer is \(ans)"

因为键盘类型是DecimalPad,没有机会输入其他0-9或。

希望这对你有帮助!!