Creating a UIAlertController without presenting it results in a warning

When creating a UIAlertController without presenting it, a warning is displayed on the console. Why is this so?

 override func viewDidLoad() { super.viewDidLoad() let _ = UIAlertController(title: "title", message: "message", preferredStyle: .Alert) } 

Attempting to load the view controller view while freeing it is not allowed and may lead to undefined behavior


Edit:

Is it safe to ignore this warning? If the UIAlertController already created and I decide not to submit / use it, what should I do?

0
source share
2 answers

You use _ (underscore) in your initialization of the UIAlertController. In Swift, using an underscore means that the variable will not be used, and in your case you create a UIAlertViewController, and because of the underscore, ARC will probably release it immediately. Try replacing the variable name _.

0
source

Try writing code in viewDidAppear, this may solve your problem

 override func viewDidAppear(animated: Bool) { super.viewDidAppear(true) let alertController = UIAlertController(title: "title", message: "message", preferredStyle: .Alert) } 
0
source

All Articles