AltBrian
AltBrian

Reputation: 2562

getting "Use of unresolved identifier 'self'" when trying to present AlertViewController

I am trying to write a function that displays a UIAlertController when a UITextField is left empty. I used the following example to display the message. At this stage, I get the following error:

Use of unresolved identifier 'self'

The code I have is below.

func displayMyAlertMessage(userMessage: String){
    let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertController.Style.alert)
    let okAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)
    myAlert.addAction(okAction)
    self.present(myAlert, animated: true, completion: nil)
}

Upvotes: 0

Views: 118

Answers (1)

matt
matt

Reputation: 535118

The problem is probably that your func is not inside any UIViewController subclass declaration. Thus there is no self (or self is not a view controller, so there is no self.present).

Upvotes: 1

Related Questions