Home > Blockchain >  How can I shorten (make more efficient) this code?
How can I shorten (make more efficient) this code?

Time:02-05

enter image description hereenter image description hereSo yeah, I don't really like asking questions here as a beginner because many of you are pretty rough, even rude with people like me just starting out, but I want to learn so I will suck it up.

I am wondering if there is a way to reduce the repetitive code below (clean it up)?

I am aware I am asking a lot and there is a lot of code below, I am just eager to learn.

Thanks for any help. Please be understanding!

    UserDefaults.standard.set(machineODInputText.text, forKey: "TBMOD")
    UserDefaults.standard.set(pipeODInputText.text, forKey: "pipeOD")
    UserDefaults.standard.set(muckUpInputText.text, forKey: "muckUp")
    UserDefaults.standard.set(jackingSpeedInputText.text, forKey: "speed")
    UserDefaults.standard.set(pipeLengthInputText.text, forKey: "pipeLength")
    UserDefaults.standard.set(driveLengthInputText.text, forKey: "driveLength")
    UserDefaults.standard.set(noOfBenoBagsInputText.text, forKey: "noOfBenoBags")
    UserDefaults.standard.set(weightOfBenoBagInputText.text, forKey: "weightOfBenoBag")
    UserDefaults.standard.set(benoQtyForTanksInputText.text, forKey: "benoQtyForTanks")
    UserDefaults.standard.set(noOfBenoBagsPerPalletInputText.text, forKey: "benoBagsPerPallet")

and

    machineODInputText.inputAccessoryView = toolBar()
    pipeODInputText.inputAccessoryView = toolBar()
    pipeLengthInputText.inputAccessoryView = toolBar()
    driveLengthInputText.inputAccessoryView = toolBar()
    muckUpInputText.inputAccessoryView = toolBar()
    jackingSpeedInputText.inputAccessoryView = toolBar()
    weightOfBenoBagInputText.inputAccessoryView = toolBar()
    noOfBenoBagsInputText.inputAccessoryView = toolBar()
    benoQtyForTanksInputText.inputAccessoryView = toolBar()
    noOfBenoBagsPerPalletInputText.inputAccessoryView = toolBar()

and

    machineODInputText.clearsOnBeginEditing = true
    pipeODInputText.clearsOnBeginEditing = true
    pipeLengthInputText.clearsOnBeginEditing = true
    driveLengthInputText.clearsOnBeginEditing = true
    muckUpInputText.clearsOnBeginEditing = true
    jackingSpeedInputText.clearsOnBeginEditing = true
    weightOfBenoBagInputText.clearsOnBeginEditing = true
    noOfBenoBagsInputText.clearsOnBeginEditing = true
    benoQtyForTanksInputText.clearsOnBeginEditing = true
    noOfBenoBagsPerPalletInputText.clearsOnBeginEditing = true

and finally

    override func viewDidAppear(_ animated: Bool) {
    if let a = UserDefaults.standard.object(forKey: "TBMOD") as? String {
        machineODInputText.text = a
    }
    
    if let a = UserDefaults.standard.object(forKey: "pipeOD") as? String {
        pipeODInputText.text = a
    }
    
    if let a = UserDefaults.standard.object(forKey: "muckUp") as? String {
        muckUpInputText.text = a
    }
    
    if let a = UserDefaults.standard.object(forKey: "speed") as? String {
        jackingSpeedInputText.text = a
    }
    
    if let a = UserDefaults.standard.object(forKey: "pipeLength") as? String {
        pipeLengthInputText.text = a
    }
    
    if let a = UserDefaults.standard.object(forKey: "driveLength") as? String {
        driveLengthInputText.text = a
    }
    
    if let a = UserDefaults.standard.object(forKey: "noOfBenoBags") as? String {
        noOfBenoBagsInputText.text = a
    }
    
    if let a = UserDefaults.standard.object(forKey: "weightOfBenoBag") as? String {
        weightOfBenoBagInputText.text = a
    }
    
    if let a = UserDefaults.standard.object(forKey: "benoQtyForTanks") as? String {
        benoQtyForTanksInputText.text = a
    }
    
    if let a = UserDefaults.standard.object(forKey: "benoBagsPerPallet") as? String {
        noOfBenoBagsPerPalletInputText.text = a
    }

CodePudding user response:

In this case, I think a good way to reduce code duplication would be to make a custom UITextField subclass, since a lot of the "duplicate" things you are doing are done to the text fields, and are really just initialising them.

I would create a subclass like this:

@IBDesignable
// you can definitely come up with a better name than I do...
class BentoniteInputField: UITextField {
    @IBInspectable
    var userDefaultKey: String = ""
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    
    func commonInit() {
        inputAccessoryView = toolBar()
        clearsOnBeginEditing = true
        if let text = UserDefaults.standard.string(forKey: userDefaultKey) {
            self.text = text
        }
    }
    
    func toolBar() -> UIView {
        // move the toolBar method here...
    }

    func writeToUserDefaults() {
        UserDefaults.standard.set(text, forKey: userDefaultKey)
    }
}

In the storyboard, you can set the text fields' class to this class:

enter image description here

then you can set the associated user default key for the text field in the storyboard too:

enter image description here

Also remember changing the types of the @IBOutlets!

Now we are only left with the code where you write to the user defaults. You can make that part a little less repetitive by putting all the text fields into an array and loop through that:

let textFields = [
    machineODInputText,
    pipeODInputText,
    // ... list all the text fields
]

for textField in textFields {
    textField.writeToUserDefaults()
}

Though personally, I wouldn't have minded just repeated writeToUserDefaults() for each text field.

  • Related