2014-06-09 13 views
9

मैं अपने अनुप्रयोगों में से एक को ओबीजे-सी से स्विफ्ट में माइग्रेट करने का प्रयास कर रहा हूं और मुझे ईमेल प्रबंधन में कोई समस्या है।
मैंने घंटों की खोज की लेकिन मुझे नहीं मिला कि इस समस्या को कैसे हल किया जाए।
असल में, मैं func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) फ़ंक्शन माइग्रेट करने का प्रयास कर रहा हूं।ईमेल भेजना - MFMailComposeResult

समस्या यह है कि स्विच के अंदर कोई विकल्प मान्य नहीं है।

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

उत्तर

19

मत भूलना कि आप भी विशिष्ट परिणाम प्रकार आप अपने चर परिणाम की तुलना कर रहे पर (स्विफ्ट के पुराने संस्करणों में .value) .rawValue उपयोग कर सकते हैं:

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

ओएमजी! वास्तव में ठीक काम करता है ... मुझे नहीं पता कि मैंने यह कैसे देखा। आपका बहुत बहुत धन्यवाद!!!! –

+0

अब आप .value – jaminguy

+0

@jaminguy के बजाय .rawValue का उपयोग करते हैं, धन्यवाद –

3

परीक्षण और काम करता है 100% स्विफ्ट 3.0 में यह बदल गया है और अब आपको ऐसा कुछ करना चाहिए:

func mailComposeController(controller: MFMailComposeViewController, 
          didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
    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.dismissViewControllerAnimated(true, completion: nil) 
} 
0

ऐसा लगता है कि स्विफ्ट 3 में 100% परीक्षण केवल 50% परीक्षण किया गया था। जब मैंने कोशिश की तो संकलक वास्तव में इसे पसंद नहीं आया था। XCode इसे ठीक करने में मेरी मदद करता है हालांकि 9-1-17 के रूप में काम करने वाले व्यक्ति को। कोड जो निम्नानुसार है संकलित है:

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) 
} 
संबंधित मुद्दे