Home > OS >  Code refactoring when there are too many conditions in swift ios
Code refactoring when there are too many conditions in swift ios

Time:09-13

I'm just a beginner with swift.

i have below code, i am trying to be able to refactor it, it seems below code is not very clean.

Hope to get help from you

class ViewController: UIViewController {

    @IBOutlet private weak var titleButton: UIButton!
    @IBOutlet private weak var titleLabel: UILabel!
    
    let nameData = [Data(name: "A"), Data(name: "B"), Data(name: "C"), Data(name: "D")]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTitle()
    }
    
    func setupTitle() {
        let title = nameData.randomElement()?.name
        if title == "A" {
            titleLabel.text = "AND"
            titleButton.backgroundColor = UIColor.black
        } else if title == "B" {
            titleLabel.text = "BOY"
            titleButton.backgroundColor = UIColor.red
        } else if title == "C" {
            titleLabel.text = "COCO"
            titleButton.backgroundColor = UIColor.blue
        } else if title == "D" {
           titleLabel.text = "DADDY"
            titleButton.backgroundColor = UIColor.gray
       }
    }
}

CodePudding user response:

Try a switch:

class ViewController: UIViewController {

    @IBOutlet private weak var titleButton: UIButton!
    @IBOutlet private weak var titleLabel: UILabel!
    
    let nameData = [Data(name: "A"), Data(name: "B"), Data(name: "C"), Data(name: "D")]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTitle()
    }
    
    func setupTitle() {
        guard let title = nameData.randomElement()?.name else { return}
        switch title {
          case "A":
            titleLabel.text = "AND"
            titleButton.backgroundColor = .black
          case "B":
            titleLabel.text = "BOY"
            titleButton.backgroundColor = .red
          case "C":
            titleLabel.text = "COCO"
            titleButton.backgroundColor = .blue
          case "D":
            titleLabel.text = "DADDY"
            titleButton.backgroundColor = .gray
        }
    }
}

CodePudding user response:

As the data is static my suggestion is to put title and color values into the struct

struct NameData {
    let name, title: String
    let color: UIColor
}

class ViewController: UIViewController {

    @IBOutlet private weak var titleButton: UIButton!
    @IBOutlet private weak var titleLabel: UILabel!
    
    let nameData = [NameData(name: "A", title: "AND", color: .black),
                    NameData(name: "B", title: "BOY", color: .red),
                    NameData(name: "C", title: "COCO", color: .blue),
                    NameData(name: "D", title: "DADDY", color: .gray)]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTitle()
    }
    
    func setupTitle() {
        let item = nameData.randomElement()!
        titleLabel.text = item.title
        titleButton.backgroundColor = item.color
    }
}

The name property is unused.

  • Related