Ошибка предупреждения в быстром

Я пишу этот код в swift и Xcode 6

@IBAction func Alert(sender : UIButton) {
  var alert : UIAlertView = UIAlertView(title: "Hey", message: "This is  one Alert",       delegate: nil, cancelButtonTitle: "Working!!")

    alert.show()
}

Xcode не показывает ошибку в компиляции.

но в Simulator APP не работает и возвращает ошибку:

(lldb)
thread 1 EXC_BAD_ACCESS(code 1 address=0x20)

Ответы

Ответ 1

В прокрутке Swift инициализатора удобства UIAlertView есть ошибка, вам нужно использовать простой инициализатор

let alert = UIAlertView()
alert.title = "Hey"
alert.message = "This is  one Alert"
alert.addButtonWithTitle("Working!!")
alert.show()

Этот код стиля более верен для языка Swift. Инициализатор удобства кажется мне более Objective-C'ish. Просто мое мнение.

Примечание: UIAlertView устарел (см. Объявление), но Swift поддерживает iOS7, и вы не можете использовать UIAlertController на iOS 7

Просмотр декларации UIAlertView в Xcode

// UIAlertView is deprecated. Use UIAlertController with a preferredStyle of   UIAlertControllerStyleAlert instead
class UIAlertView : UIView {

Предупреждение в Swift iOS 8 Only

var alert = UIAlertController(title: "Hey", message: "This is  one Alert", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)

Обновление для Swift 4.2

        let alert = UIAlertController(title: "Hey", message: "This is  one Alert", preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertAction.Style.default, handler: nil))
        self.present(alert, animated: true, completion: nil)