Instance member 'present' cannot be used on type 'Service'

class Service: UIViewController {
   
  static func showError(_ message:String) {
    // Create new Alert
    let dialogMessage = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
     
    // Create OK button with action handler
    let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
      print("Ok button tapped")
    })
     
    //Add OK button to a dialog message
    dialogMessage.addAction(ok)
    // Present Alert to
    self.present(dialogMessage , animated: true, completion: nil)
     
  }
}

why i get error in this line:

self.present(dialogMessage , animated: true, completion: nil)

the error:

Instance member 'present' cannot be used on type 'Service'

why?

Replies

This is due to your function showError() being static (so a Class function), but present() from UIViewController is an instance method. The showError() function should not be static. Try removing the static keyword, then it should work.