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
}
}
}