Home > Net >  How to add Nil to UIAlertAction
How to add Nil to UIAlertAction

Time:09-26

I have to put Nil to getIDColor, but I don't know right way to do that.

var getIDColor: Int?

let colorButton = UIAlertAction(title: "Select Color", style: .default) { _ in
    self.openColorTVC(categId: self.getIDColor)
}

CodePudding user response:

If you mean how to define a func with optional parameter, here is the answer:

enter image description here

CodePudding user response:

following the answer by Farhad Bagherzadeh I used guard statement for categId

func openColorTVC(categId: Int?) {
        guard categId != nil
        else {
            return openColorTVC(categId: 1)
        }
        if let checkColorId = categId {
            let colorSB = UIStoryboard(name: "Main", bundle: nil)
            
            if let destinCSB = colorSB.instantiateViewController(withIdentifier: "colorsID") as? ColorsTVC {
                destinCSB.categorColors = checkColorId
                navigationController?.pushViewController(destinCSB, animated: true)
            }
        }
    }   
  • Related