1

I'm using a custom class of AlertController Here I have an init

Self.init(message: "test1")

Then in the some condition, I have to change the alert message.

If someCondition {
 //here
} 

I can call another time Self.init(message: "test2"), but it's not a good way, could anyone recommend a good way to do that?

Thanks

  • Don't do an init, but just set that property as a `var`, not a `let`, and do `thatObject.message = "newValue"`? – Larme Dec 12 '19 at 16:44
  • What do you mean by "a custom class of AlertController?" Is this a subclass of `UIAlertController`? (That class is specifically not intended to be subclassed. "The UIAlertController class is intended to be used as-is and does not support subclassing.") – Rob Napier Dec 12 '19 at 17:56

1 Answers1

1

You can modify the message property after initialisation:

class Alert: UIAlertController {

}

let alert = Alert(title: "Title", message: nil, preferredStyle: .alert)
alert.message = "Message"
shbedev
  • 1,875
  • 17
  • 28
  • In my case, I am already in the Alert class. I can do it, but the message in the init wont'e be changaed – Lifetime Email Dec 12 '19 at 16:48
  • Try this one - https://stackoverflow.com/questions/33092628/how-can-i-override-convenience-init-in-uialertcontroller-for-swift – shbedev Dec 12 '19 at 16:59