Swift4:强制转换从 'Data?' 到 'Data' 只解包选项;你是想使用 '!' 吗?

标签 swift swift4 forced-unwrapping

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

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

最佳答案

假设贷款中的所有类型都符合预期的类型,以下应该有效:

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
}

关于Swift4:强制转换从 'Data?' 到 'Data' 只解包选项;你是想使用 '!' 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47108915/

相关文章:

ios - 在 Swift 4 中自定义 UISearchBar

ios - UICollectionViewCell 中的渐变 BG

Swift 标记 : Callback argument breaks documentation string

ios - 如何在快速启动时加载详细 View Controller (从 Split View)?

ios - 尝试将 NSDate 转换为 Date 并收到警告

coding-style - Swift 中的早期回归/黄金路径

swift - 为什么 SwiftyJSON 为其常量创建隐式解包可选?

swift - 为什么使用隐式解包或强制解包让应用程序在某个阶段崩溃没有好处?

ios - 如何从 swift 4.2 中的通知传递 url 以在 webview 中打开

ios - 枚举内存使用