how to resolve Cannot find 'XXXX' in scope

Hi im new at developing and i got 2 errors i can't seem to find an answer on google. here is my code and the errors are
Cannot find 'self' in scope
Cannot find 'present' in scope

//
// ViewController.swift
// Diabell
//
// Created by Richard Klug on 14/04/2021.
//

import UIKit
import MessageUI

class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
  }
   
  @IBAction func emailbButtonTapped( sender: Any) {
   showMailComposer()
  }
}
  func showMailComposer() {
    guard MFMailComposeViewController.canSendMail() else{
      return
    }
    let composer = MFMailComposeViewController()
    composer.mailComposeDelegate = self
    composer.setToRecipients(["richard.klug@diabell.se"])
    composer.setSubject("Diabell App Help")
    composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false)
    present(composer, animated: true)
     
}



  extension ViewController: MFMailComposeViewControllerDelegate {
    func mailComposeController(
controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
      if let _ = error {
        controller.dismiss(animated: true)
        return
      }
      switch result {
      case .cancelled:
        print ("Cancelled")
      case .failed:
        print ("Failed to send")
      case .saved:
        print ("Saved")
      case .sent:
        print ("Email Sent")
      }
      controller.dismiss(animated: true)
    }
}

Answered by Claude31 in 671263022
You have 2 errors :
Code Block
//
// ViewController.swift
// Diabell
//
// Created by Richard Klug on 14/04/2021.
//
import UIKit
import MessageUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func emailbButtonTapped( sender: Any) {
showMailComposer()
}
}
func showMailComposer() {
guard MFMailComposeViewController.canSendMail() else {
return
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["richard.klug@diabell.se"])
composer.setSubject("Diabell App Help")
composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false)
present(composer, animated: true)
}
  • need to conform to MFMailComposeViewControllerDelegate

Code Block
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
  • Extra closing curly bracket line 21: remove it and put it line 34

Hence, the code after line 22 was out of the class, hence self not found and present does not apply to anything. So compiler tried to interpret as your own func.

Correct code:
Code Block
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func emailbButtonTapped( sender: Any) {
showMailComposer()
}
func showMailComposer() {
guard MFMailComposeViewController.canSendMail() else{
return
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["richard.klug@diabell.se"])
composer.setSubject("Diabell App Help")
composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false)
present(composer, animated: true)
}
}

If that's OK, don't forget to mark the correct answer to close the thread.

Note: when you post code, please use the code formatter tool (<>).

Accepted Answer
You have 2 errors :
Code Block
//
// ViewController.swift
// Diabell
//
// Created by Richard Klug on 14/04/2021.
//
import UIKit
import MessageUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func emailbButtonTapped( sender: Any) {
showMailComposer()
}
}
func showMailComposer() {
guard MFMailComposeViewController.canSendMail() else {
return
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["richard.klug@diabell.se"])
composer.setSubject("Diabell App Help")
composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false)
present(composer, animated: true)
}
  • need to conform to MFMailComposeViewControllerDelegate

Code Block
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
  • Extra closing curly bracket line 21: remove it and put it line 34

Hence, the code after line 22 was out of the class, hence self not found and present does not apply to anything. So compiler tried to interpret as your own func.

Correct code:
Code Block
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func emailbButtonTapped( sender: Any) {
showMailComposer()
}
func showMailComposer() {
guard MFMailComposeViewController.canSendMail() else{
return
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["richard.klug@diabell.se"])
composer.setSubject("Diabell App Help")
composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false)
present(composer, animated: true)
}
}

If that's OK, don't forget to mark the correct answer to close the thread.

Note: when you post code, please use the code formatter tool (<>).

Thanks!
how to resolve Cannot find 'XXXX' in scope
 
 
Q