如何将2个textfields togethers添加为int(swift3) [英] how to add 2 textfields togethers as int (swift3)
本文介绍了如何将2个textfields togethers添加为int(swift3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将2个文本字段转换为整数,然后将它们一起添加。我还想在日志中打印总和。
I am trying to convert 2 textfields to ints and then add them together. I would also like to print the sum in the log.
let jake = t3.text! + t4.text!
推荐答案
在 Swift中将文本转换为Int 以及我们可以做的补充......
Convert text into Int in Swift and an addition like we can do...
//set before this condition Validation for Text field
let sum = (Int(textFirst.text ?? "0")! + Int(textSecond.text ?? "0"))!
print(sum) //Output here
//MARK: - Text Field Delegate Method for Input validation
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
let allowedCharacters = CharacterSet.decimalDigits
let characterSet = CharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet)
}
这篇关于如何将2个textfields togethers添加为int(swift3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文