2014-06-09 5 views
9

Obj-C에서 Swift로 내 응용 프로그램 중 하나를 마이그레이션하려고하는데 전자 메일 관리에 문제가 있습니다.
몇 시간 씩 검색했지만이 문제를 해결하는 방법을 찾지 못했습니다.
기본적으로 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

OMG! 정말 잘 작동 ... 어떻게 보지 못했는지 모르겠다. 대단히 감사합니다 !!!! –

+0

이제는 .value 대신 .rawValue를 사용합니다. – jaminguy

+0

@jaminguy 업데이트 됨, thanks –

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) 
}