UIAlertAction handler

I am writing UIAlert and UIAlertAction and I want to add performSeque to handler in UIAlertAction but I keep getting this error: "Cannot use instance member 'performSegue' within property initializer; property initializers run before 'self' is available"

here is my code
Code Block
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
  
let action = UIAlertAction(title: "Title", style: .default, handler: { action in
    performSegue(withIdentifier: "goToVC", sender: nil)
  })

Answered by Claude31 in 645082022
If I need to have alert as a class property, I then declare it as
Code Block
var alert : UIAlertController?
and initialize in viewDidLoad.

Otherwise, I do as OOPer described it.
Seems you have written your code right under the class definition as property declaration.
You need to move it into some instance method.
Code Block
class YourViewController: UIViewController {
//Do not put the code directly under the class:
//let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
//let action = UIAlertAction(title: "Title", style: .default, handler: { action in
//    performSegue(withIdentifier: "goToVC", sender: nil)
//})
func someMethod() {
//...
//Put the code in a method:
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
let action = UIAlertAction(title: "Title", style: .default, handler: { action in
    performSegue(withIdentifier: "goToVC", sender: nil)
})
//...
}
//...
}


Accepted Answer
If I need to have alert as a class property, I then declare it as
Code Block
var alert : UIAlertController?
and initialize in viewDidLoad.

Otherwise, I do as OOPer described it.
Okay so first of all sorry, sorry I put the checkmark by mistake 😅 and when I put the alert and action in viewDidLoad() I get that yellow triangle saying this for alert "Initialization of immutable value 'alert' was never used; consider replacing with assignment to '' or removing it", and for action "Initialization of immutable value 'action' was never used; consider replacing with assignment to '' or removing it". I tried both ways and it always showed up.

Sorry if my questions and answers are weird I'm new to this and still learning

I put the alert and action in viewDidLoad()

Never put such code in viewDidLoad(), you instantiate UIAlertController just before you present it. Do not have it as a property.

If you want to reduce some lines for repeatedly used alerts, you should create a method, not a property.
Code Block
class ViewController: UIViewController {
//...
func createTheAlert() -> UIAlertController {
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
let action = UIAlertAction(title: "Title", style: .default, handler: { action in
self.performSegue(withIdentifier: "goToVC", sender: nil)
})
alert.addAction(action)
//...
return alert
}
@IBAction func someActionMethod(_ sender: Any) {
//...
let alert = createTheAlert()
present(alert, animated: true, completion: nil)
}
@IBAction func anotherActionMethod(_ sender: Any) {
//...
let alert = createTheAlert()
present(alert, animated: true, completion: nil)
}
//...
}


If you do not want to make your questions misunderstood, you should better include enough code. Exactly the same lines will work in some place and some context, but they will not in other cases. At least, full method including the lines.
UIAlertAction handler
 
 
Q