[Swift] NSClassFromString("UIAlertController") crashes on iOS 7 with optimizations turned on
| Originator: | wirth_caesar | ||
| Number: | rdar://19289025 | Date Originated: | 18-Dec-2014 |
| Status: | Closed | Resolved: | Yes |
| Product: | Developer Tools | Product Version: | Version 6.2 (6C86e) |
| Classification: | Reproducible: | Always |
Summary:
In iOS 8, we have UIAlertController. In iOS 7, we do not. I wrote a wrapper class to handle these, and inside it. It was working with optimizations turned off (-Onone), but then when trying to run the same code with optimizations turned on (-O), I was getting a number of different errors, but it was always crashing.
Steps to Reproduce:
The following code triggers the error:
1 let hasAlert = NSClassFromString("UIAlertController")
2 // NSLog("What is alert? \(hasAlert)")
3 if hasAlert != nil {
4 NSLog("We have UIAlertController")
5 // let alert = UIAlertController()
6 // self.presentViewController(alert, animated: true, completion: nil)
7 } else {
8 NSLog("We do not have UIAlertController")
9 }
Running this exact code on iOS 7, with optimizations turned on, will log out:
"We have UIAlertController" which is clearly incorrect.
Uncommenting lines 5 and 6 will produce an error about trying to present nil onto a view controller.
Uncommenting line 2 will produce an EXC_BAD_ACCESS on line 1. This can also be reproduced by casting "hasAlert" to AnyClass? AnyClass!, or trying to use it in any sense that is more than checking its nilness.
Expected Results:
The NSClassFromString method to return AnyClass? rather than AnyClass!, and to actually return the correct value when code optimizations are turned on. That is, not crash.
Actual Results:
It crashes.
Version:
Version 6.2 (6C86e)
Notes:
Configuration:
iPhone 5s (7.1) Simulator
Update:
I was asked to provide crash logs, so I sent them the following data:
http://s000.tinyupload.com/index.php?file_id=55773461656617445657
Update:
Fixed in Xcode 6.3 beta 3 (6D543q)
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!