为int总数添加一个字符串和一个int(Swift4)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为int总数添加一个字符串和一个int(Swift4)相关的知识,希望对你有一定的参考价值。

我想要做的是将2个文本字段添加到一起,但我想将非int与int一起添加。所以我想沿着+ 2 = 5的行做一些事情。我想将int a定义为等于3.所以a = 3,a + 2应该在文本字段中等于5。

@IBOutlet var txt1 : UITextfield!
@IBOutlet var txt2 : UITextfield!
@IBOutlet var total : UITextfield!

@IBAction func addTogether() {
//if txt1 = a then txt1 = 3 
}
答案

以下是演示,无需在文本字段中明确插入“a”即可完成所描述的操作。有3个领域有a + b = sum规则。其中哪些将被修改取决于您正在编辑的字段。因此,如果您设置a然后b,您将看到sum被修改。如果你然后开始编辑sum你将看到a修改取决于bsum

所有这些都是在代码中完成的,因此我们鼓励您创建一个新项目并用此代码替换您的ViewController并尝试它。我希望这足以让你走上你需要的道路,但请注意,创建这样的系统通常需要完成一些工作。

import UIKit

class ViewController: UIViewController {

    private var firstField: UITextField!
    private var secondField: UITextField!
    private var sumField: UITextField!

    private var currentlyUsedField: UITextField?
    private var previouslyUsedField: UITextField?

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor(white: 0.92, alpha: 1.0)

        self.firstField = {
            let field = UITextField(frame: CGRect(x: 12.0, y: 60.0 + (44.0+12.0)*0.0, width: view.bounds.width-24.0, height: 44.0))
            field.keyboardType = .decimalPad
            field.backgroundColor = UIColor.white
            field.addTarget(self, action: #selector(onFieldDidChange), for: .editingChanged)
            view.addSubview(field)
            return field
        }()

        self.secondField = {
            let field = UITextField(frame: CGRect(x: 12.0, y: 60.0 + (44.0+12.0)*1.0, width: view.bounds.width-24.0, height: 44.0))
            field.keyboardType = .decimalPad
            field.backgroundColor = UIColor.white
            field.addTarget(self, action: #selector(onFieldDidChange), for: .editingChanged)
            view.addSubview(field)
            return field
        }()

        self.sumField = {
            let field = UITextField(frame: CGRect(x: 12.0, y: 60.0 + (44.0+12.0)*2.0, width: view.bounds.width-24.0, height: 44.0))
            field.keyboardType = .decimalPad
            field.backgroundColor = UIColor.white
            field.addTarget(self, action: #selector(onFieldDidChange), for: .editingChanged)
            view.addSubview(field)
            return field
        }()
    }

    private func convertStringToDouble(_ string: String?) -> Double? {
        // Maybe use some formatters here
        let number = NSDecimalNumber(string: string)
        let doubleNumber = number.doubleValue
        return doubleNumber.isNaN ? nil : doubleNumber
    }

    private func convertDoubleToString(_ value: Double) -> String {
        // Maybe use some formatters here
        return String(value)
    }

    @objc private func onFieldDidChange(_ sender: UITextField) {
        // Check if we need to update a chain of used fields
        if sender !== currentlyUsedField {
            previouslyUsedField = currentlyUsedField
            currentlyUsedField = sender
        }

        // At least 2 fields must have been modified
        guard let currentlyUsedField = currentlyUsedField, let previouslyUsedField = previouslyUsedField else { return }

        // We need to know if field we are modifying is a sum or not
        let fieldToModify = [firstField, secondField, sumField].filter { $0 !== currentlyUsedField && $0 != previouslyUsedField }.first!
        if fieldToModify === sumField {
            // Show the sum
            if let left = convertStringToDouble(previouslyUsedField.text), let right = convertStringToDouble(currentlyUsedField.text) {
                fieldToModify?.text = convertDoubleToString(left+right)
            } else {
                fieldToModify?.text = "UNDEFINED"
            }
        } else {
            let leftField: UITextField = fieldToModify == firstField ? secondField : firstField
            if let sum = convertStringToDouble(sumField.text), let subtract = convertStringToDouble(leftField.text) {
                fieldToModify?.text = convertDoubleToString(sum-subtract)
            } else {
                fieldToModify?.text = "UNDEFINED"
            }
        }
    }


}

以上是关于为int总数添加一个字符串和一个int(Swift4)的主要内容,如果未能解决你的问题,请参考以下文章

在 for 循环中添加整数并打印总数

如何检查String是否是Swift中的Int?

如何在 Swift 中将 Int 转换为字符

Swift 中 Int 的前导零

Swift 将可选字符串转换为 Int

Swift 4 将字符串转换为 UnsafeMutablePointer<Int8>