0

I have 2 textFields with Strings that represent URLs. If the textfield is empty, it saves a custom string ("no link A"), if filled, it needs to be valid, otherwise it shows an alert.

Is there a cleaner way to do this rather than using so many "if statements"?

linkA = linkATextField.text      
linkB = linkBTextField.text

     @IBAction func doneButton(_ sender: Any) {

            if linkA.isEmpty && linkB.isEmpty {
                linkA = "no link A"
                linkB = "no link B"
                saveData()
            } else {
                if linkA.isEmpty == false && linkB.isEmpty == false  {
                    if linkA.isValidURL && linkB.isValidURL {
                         saveData()
                    } else {
                        showErrorAlert()
                    }
                }
                if linkA.isEmpty && linkB.isEmpty == false {
                    linkA = "no link A"
                    if linkB.isValidURL {
                         saveData()
                    } else {
                        showErrorAlert()
                    }
                }
                if linkA.isEmpty == false && linkB.isEmpty {
                    linkB = "no link B"
                    if linkA.isValidURL {
                         saveData()
                    } else {
                        showErrorAlert()
                    }
                }
            }

        }
Maruta
  • 1,063
  • 11
  • 24

4 Answers4

5

alternate you can use Switch case , try this

 @IBAction func doneButton(_ sender: Any){
        switch (linkA.isEmpty, linkB.isEmpty) {
        case (true, true):
            // no link A, B
             saveData()
            break
          case (false, false):
             // no link A, B
             if linkA.isValidURL, linkB.isValidURL {
                 saveData()
              }else { showErrorAlert() }
            break
             case (true, false):
              // no link A
            linkB.isValidURL  ? saveData() : showErrorAlert()
            break
            case (false, true):
             // no link B
            linkA.isValidURL  ? saveData() : showErrorAlert()
            break
        default:
           showErrorAlert()
             break
        }
    }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

You could use a switch statement to make your logic easier to understand

switch (linkA.isEmpty, linkB.isEmpty) {
case (true, true):
    // do something for both empty
    print("both are empty")
case (true, false):
    // do something for linkA being empty
    print("linkA is empty")
case (false, true):
    // do something for linkB being empty
    print("linkB is empty")
case (false, false):
    // do something for both NOT empty
    print("both are not empty")
}

You could then create a helper function that shows the alert rather than repeat the same code multiple times.

Andrew
  • 26,706
  • 9
  • 85
  • 101
0

You can try one more alternative way

 @IBAction func doneButton(_ sender: Any) {

    let isLinkAValid = linkA.isEmpty ? true : linkA.isValidURL
    let isLinkBValid = linkB.isEmpty ? true : linkB.isValidURL

    let linkAText = linkA.isEmpty ? "No Link A" :  linkATextField.text!
    let linkBText = linkB.isEmpty ? "No Link B" :  linkBTextField.text!

    if isLinkAValid && isLinkBValid {
        saveData(strLinkA: linkAText, strLinkB: linkBText)
    }
    else {
        showErrorAlert()
    }
}
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
0

You can use Rx to manage it, here is an example

    @IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var textField2: UITextField!
    @IBOutlet weak var actionButton: UIButton!
    private var disposedBag = DisposeBag()

private func startSettings() {
    let obs1 = textField1.rx.text.orEmpty
    let obs2 = textField2.rx.text.orEmpty
    Observable
        .combineLatest(obs1, obs2)
        .map { s1, s2 in
            return s1.count > 0 && s2.count > 0
    }
    .debounce(.seconds(1), scheduler: MainScheduler.instance)
    .bind(to: actionButton.rx.isEnabled)
    .disposed(by: disposedBag)
}

Action button will be disabled if textfields are empty, waiting for 1 second for any changes in textfields, in .map{}, you can show your alerts if you want to

Alexandr Kolesnik
  • 1,929
  • 1
  • 17
  • 30