Alert error in swift [duplicate] Alert error in swift [duplicate] swift swift

Alert error in swift [duplicate]


There is a bug in the Swift shim of the UIAlertView convenience initializer, you need to use the plain initializer

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

This style code feels more true to the Swift Language. The convenience initializer seems more Objective-C'ish to me. Just my opinion.

Note: UIAlertView is deprecated (see declaration) but Swift supports iOS7 and you can not use UIAlertController on iOS 7

View of UIAlertView Declaration in Xcode

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

An Alert in 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)

Update for 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)