3

at the last update to Swift 4, im always getting the same error at the same spot and i don't know how to clear it...

if tried to use ! instead of ? but the error keep going in the opposite direction.

the Error is with both Date? and Data?

thats the code:

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
    }

all other errors are gone, but this one keeps my head smoking.

luk2302
  • 55,258
  • 23
  • 97
  • 137
Sjero Markus
  • 111
  • 8
  • Not related to the issue, but don't call `reloadData` and `delete/insertRows` consecutively. `deleteRows` does update the table view with animation so `reloadData` is redundant. Remove the line. Remove also the lines `beginUpdates() / endUpdates()`. They are not needed either. And consider to declare `loan` as non-optional. The huge amount of question and exclamation marks is annoying. – vadian Nov 04 '17 at 09:53
  • Hello vadian, tried your suggestions but it crashed my app - it seems that they are need to refresh the table and update the view - with these lines, everything works, without one line, the app crashes! but thanks anyway! the answer from luk2302 worked like a charm :) – Sjero Markus Nov 04 '17 at 10:21

1 Answers1

1

Assuming all types inside loan match the expected types the following should work:

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
}
luk2302
  • 55,258
  • 23
  • 97
  • 137