MFMailComposeViewControllerDelegate: Cannot declare conformance to 'NSObjectProtocol' in Swift

41 Views Asked by At

I tried to implement MFMailComposeViewControllerDelegate to use mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) method but get the error:

Cannot declare conformance to 'NSObjectProtocol' in Swift; 'SettingsViewModel' should inherit 'NSObject' instead

What could be the problem?

Here is the code:

extension SettingsViewModel: MFMailComposeViewControllerDelegate {
    public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }
}
1

There are 1 best solutions below

0
On

The MFMailComposeViewControllerDelegate protocol extends the NSObjectProtocol protocol. You are only allowed to conform to NSObjectProtocol by subclassing the NSObject class.

You need to find the declaration of SettingsViewModel in your source code and change it to extend NSObject:

class SettingsViewModel: NSObject {
   ...