UIAlertView in Swift, getting EXC_BAD_ACCESS

ios, swift, xcode

Solution

Try the following code

let alert = UIAlertView()
alert.title = "Title"
alert.message = "My message"
alert.addButtonWithTitle("Ok")
alert.show()

But in iOS 8

`UIAlertView` is deprecated. So use `UIAlertController` with a `preferredStyle` of `UIAlertControllerStyleAlert`. It should be:

var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)

Check the above code, are you getting same error or not ?

Problem

First and foremost, I'm quite aware that Xcode 6 and the Swift language are in Beta and are prone to errors; however, this particular one seems to be something strange as everything else I've tried so far seems to work fine. If this is not appropriate for StackOverflow, I will gladly remove the question. I've begin playing with Xcode 6/Swift (preparing for its release) and it has been an extraordinarily enjoyable experience compared to what I thought it would be. That being said, one issue in porting a "training" style app I like to do is that I can't seem to generated a UIAlertView due to `EXC_BAD_ACCESS` the code in question is: ``` override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) var alert = UIAlertView(title: "Title", message: "Message", delegate: nil, cancelButtonTitle: "OK") // EXC_BAD_ACCESS here alert.show() } ``` On the line that creates the UIAlertView I get an `EXC_BAD_ACCESS` because `[UIAlertView retain]` was called on a deallocated instance. Again, I'm chalking this up to the beta banner but was curious if I was doing something wrong or if anyone else has run into similar issues.

Original source