【问题标题】:Swift4: Forced cast from 'Data?' to 'Data' only unwraps optionals; did you meant to use '!'?Swift4:从“数据”强制转换? to 'Data' 只解开可选项;你的意思是使用'!'?
【发布时间】:2017-11-04 08:30:52
【问题描述】:

在 Swift 4 的最后一次更新中,我总是在同一个地方遇到同样的错误,我不知道如何清除它......

如果尝试使用 !代替 ?但错误继续向相反的方向发展。

两个日期都有错误?和数据?

代码如下:

let done = UITableViewRowAction(style: .normal, title: doneTitle) { action, index in
        tableView.beginUpdates() // Beginne mit dem Update


        // error in the following line
        self.appDelegate.loanResource.editLoan(withObjID: (loan?.objectID)!, andName: (loan?.name)!, andAmount: (loan?.amount)!, andNote: (loan?.note)!, andCreated: loan?.created as! Date, andDue: (loan?.due) as! Date, andDone: nowDone, andImage: loan?.image as! Data, andContactInfoMail: (loan?.contactInfoMail)!, andContactInfoNumber: (loan?.contactInfoNumber)!, andChargeMode: (loan?.chargeMode)!, andChargeAmount: (loan?.chargeAmount)!, andReminder: (loan?.reminder)!, andReminderID: (loan?.reminderID)!)


        UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [(loan?.reminderID)!]) // Reminder entfernen, weil der Betrag zurück gezahlt wurde
        tableView.reloadData()
        self.loanList?.remove(at: indexPath.row) // Datensatz aus der Variable entfernen
        tableView.deleteRows(at: [indexPath], with: .fade) // Datensatz ausblenden
        tableView.endUpdates() // Update beenden
        vc?.setTotalAmount() // Gesamtwert aktualisieren
    }

所有其他错误都消失了,但这个错误让我头疼。

【问题讨论】:

  • 与问题无关,但不要连续调用reloadDatadelete/insertRowsdeleteRows 确实用动画更新了表格视图,所以reloadData 是多余的。删除线。同时删除行beginUpdates() / endUpdates()。它们也不需要。并考虑将loan 声明为非可选。大量的问号和感叹号令人讨厌。
  • 您好 vadian,尝试了您的建议,但它使我的应用程序崩溃 - 似乎他们需要刷新表格并更新视图 - 使用这些行,一切正常,没有一行,应用程序崩溃!但无论如何谢谢!来自 luk2302 的答案就像一个魅力:)

标签: swift swift4 forced-unwrapping


【解决方案1】:

假设贷款中的所有类型都与预期的类型匹配,以下应该可以工作:

if let loan = loan {
    self.appDelegate.loanResource.editLoan(withObjID: loan.objectID, andName: loan.name, andAmount: loan.amount, andNote: loan.note, andCreated: loan.created, andDue: loan.due, andDone: nowDone, andImage: loan.image, andContactInfoMail: loan.contactInfoMail, andContactInfoNumber: loan.contactInfoNumber, andChargeMode: loan.chargeMode, andChargeAmount: loan.chargeAmount, andReminder: loan.reminder, andReminderID: loan.reminderID)
    // more to to with the unwrapped loan
} else {
    // loan was nil
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多