2014-06-09 1 views
9

Ich versuche eine meiner Anwendungen von Obj-C nach Swift zu migrieren und habe ein Problem mit der E-Mail-Verwaltung.
Ich suchte nach Stunden, aber ich habe nicht gefunden, wie dieses Problem zu lösen.
Grundsätzlich versuche ich die func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) Funktion zu migrieren.E-Mails senden - MFMailComposeResult

Das Problem ist, dass keine Optionen innerhalb des Schalters gültig sind.

func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) 
{ 
    switch result.value 
    { 
     case CUnsignedInt(MFMailComposeResultCancelled): 
      var alert = UIAlertController(
       title: NSLocalizedString("sendingStatus", tableName: "LocalizationFile", comment:"sendingStatus"), 
       message: NSLocalizedString("emailCancelledByUser", tableName: "LocalizationFile", comment:"emailCancelledByUser"), 
       preferredStyle: UIAlertControllerStyle.Alert) 
      self.presentViewController(alert, animated: true, completion: nil) 
     case MFMailComposeResult(MFMailComposeResultFailed): 
      var alert = UIAlertController(
       title: NSLocalizedString("sendingStatus", tableName: "LocalizationFile", comment:"sendingStatus"), 
       message: NSLocalizedString("emailSentFailed", tableName: "LocalizationFile", comment:"emailSentFailed"), 
       preferredStyle: UIAlertControllerStyle.Alert) 
      self.presentViewController(alert, animated: true, completion: nil) 
     case MFMailComposeResultSaved: 
      var alert = UIAlertController(
       title: NSLocalizedString("sendingStatus", tableName: "LocalizationFile", comment:"sendingStatus"), 
       message: NSLocalizedString("emailSaved", tableName: "LocalizationFile", comment:"emailSaved"), 
       preferredStyle: UIAlertControllerStyle.Alert) 
      self.presentViewController(alert, animated: true, completion: nil) 
     default: 
      var alert = UIAlertController(
       title: NSLocalizedString("sendingStatus", tableName: "LocalizationFile", comment:"sendingStatus"), 
       message: NSLocalizedString("emailNotSent", tableName: "LocalizationFile", comment:"emailNotSent"), 
       preferredStyle: UIAlertControllerStyle.Alert) 
      self.presentViewController(alert, animated: true, completion: nil) 
    } 
} 

enter image description here

Antwort

19

Vergessen Sie nicht, dass Sie auch .rawValue (.value in älteren Versionen von Swift) auf den spezifischen Ergebnistypen verwenden, können Sie Ihr Variable Ergebnis sind zu vergleichen:

var result:MFMailComposeResult = MFMailComposeResultCancelled 

    switch(result.value) { // <-- Here, note .value is being used 
     case MFMailComposeResultCancelled.value: // <-- And here as well! 
      print("Cancelled") 
     default: 
      print("Default") 
    } 
+0

OMG! Wirklich funktioniert gut ... Ich weiß nicht, wie ich das nicht gesehen habe. Vielen Dank!!!! –

+0

Sie verwenden nun .rawValue anstelle von .value – jaminguy

+0

@jaminguy Aktualisiert, danke –

3

Getestet und funktioniert 100% In Swift 3.0 ist dies geändert und jetzt sollten Sie etwas wie das machen:

0

Es scheint mir, dass die 100% getestet in Swift 3 nur zu etwa 50% getestet wurde. Wenn ich es versuchte, mochte der Compiler es nicht wirklich. XCode hilft mir dabei, es zu beheben, das am 9-1-17 funktioniert hat. Der folgende Code ist der Code, der schließlich kompiliert wurde:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?){ 
    switch result.rawValue { 
    case MFMailComposeResult.cancelled.rawValue: 
     print("Mail cancelled") 
    case MFMailComposeResult.saved.rawValue: 
     print("Mail saved") 
    case MFMailComposeResult.sent.rawValue: 
     print("Mail sent") 
    case MFMailComposeResult.failed.rawValue: 
     print("Mail sent failure: %@", [error!.localizedDescription]) 
    default: 
     break 
    } 
    // Dismiss the mail compose view controller. 
    controller.dismiss(animated: true, completion: nil) 
}