MailComposeViewController错误:意外发现nil

时间:2016-08-24 16:30:43

标签: swift null runtime-error optional-values

我正在尝试将我发送的电子邮件的电子邮件和时间保存到表格视图中。电子邮件发送正常,但每次单击发送按钮时,我都会收到此错误(错误:在展开Optional值时意外发现nil)。我不确定我的代码中的错误在哪里。

发送按钮的代码:

//SentEmails Array
var sentEmails = [emailObject]()

//Default email
var sentEmailTo = "email@email.com"

//Dismiss Buttons for Mail Composer
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError?) {

    //Check for errors
   if let error = error {
        print("Error: \(error)")
        return
    }

    switch result {
    case MFMailComposeResultCancelled:
        print("Mail Cancelled")
    case MFMailComposeResultSaved:
        print("Mail Saved")
    case MFMailComposeResultSent:

     //Save information to tableview
        let emailSent = emailObject(sentTo: sentEmailTo, timeSent: NSDate())
        sentEmails.append(emailSent)
        tableView.reloadData()

        print("Mail Sent")

    case MFMailComposeResultFailed:
        print("Mail sent failure: \(error)")
    default:
        break
    }

    controller.dismissViewControllerAnimated(true, completion: nil)
}

我的手机代码:

//Cell Configuration
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)

    let row = indexPath.row

    let dateFormat = NSDateFormatter()
    dateFormat.dateStyle = .ShortStyle
    dateFormat.timeStyle = .ShortStyle
    cell.textLabel?.text = sentEmails[row].sentTo
    cell.detailTextLabel?.text = dateFormat.stringFromDate(sentEmails[row].timeSent)



    return cell
}

错误:

enter image description here

*不知道我能做到这个大声笑,这里是错误细节:

enter image description here

2 个答案:

答案 0 :(得分:1)

发生崩溃时,您可以按1的行, enter image description here

它将显示有关崩溃发生位置的更多信息(在ViewController文件中)。你能发布一个截图吗?

抱歉,我无法发布图片作为评论。一旦我们了解更多,我们就会编辑答案。

答案 1 :(得分:0)

你是否在接口生成器中连接了tableView?如果没有,您需要为表格定义数据源,委托和出口(" Gratitude Stream"位来自我的示例,但您的来自您自己的视图控制器)。 enter image description here

相关问题