Неверный экземпляр NSError - PullRequest
       48

Неверный экземпляр NSError

0 голосов
/ 14 октября 2018

Я получаю странное сообщение в консоли после запуска этого кода:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let pickUp = sortedPickUps[indexPath.item]

    let alert = UIAlertController(title: "Delete", message: "Permanently delete your order?", preferredStyle: .alert)
    let yes = UIAlertAction(title: "Yes", style: .destructive) { (actio) in

        DispatchQueue.main.async {
            self.pickUpController.delete(pickUp: pickUp)
            self.pickUpController.deleteFirebasePickUp(pickUp: pickUp, completion: { (error) in
                if let error = error {

                    NSLog("Error deleting pick up \(error)")
                }
            })
            self.sortedPickUps = self.pickUpController.pickUps.sorted(by: { $0.timestamp > $1.timestamp })

            self.collectionView.reloadData()
        }
            self.sendNotification()
    }
    let no = UIAlertAction(title: "No", style: .default) { (action) in }

    alert.addAction(yes)
    alert.addAction(no)
    present(alert, animated: true, completion: nil)
}

URLSession в deleteFirebasePickUp выглядит так

 URLSession.shared.dataTask(with: request) { (data, _, error) in
        if let error = error {
            NSLog("Error deleting entry from server: \(error)")
            DispatchQueue.main.async {
                completion(error)
            }
            return
        }
        DispatchQueue.main.async {
            guard let index = self.pickUps.index(of: pickUp) else {
                NSLog("Something happened to the entry")
                completion(NSError())
                return
            }
            self.pickUps.remove(at: index)
            completion(nil)
        }
    }.resume()

Приложениене вылетает, а фактически делает то, что должен делать - удаляет звукосниматель с устройства и из базы данных.Ошибка: error = (Error?) domain: nil - code:0

Отладчик говорит:

Something happened to the entry 2018-10-13 14:14:37.608435-0700 chute[36293:4218837] -[NSError init] called; this results in an invalid NSError instance. It will raise an exception in a future release. Please call errorWithDomain:code:userInfo: or initWithDomain:code:userInfo:. This message shown only once.

Есть идеи?

1 Ответ

0 голосов
/ 14 октября 2018

Перечитайте сообщение отладчика:

[NSError init] called; this results in an invalid NSError instance.

Если вы перечитаете свой второй блок кода

     DispatchQueue.main.async {
        guard let index = self.pickUps.index(of: pickUp) else {
            NSLog("Something happened to the entry")
            completion(NSError()) // THIS LINE
            return
        }
        self.pickUps.remove(at: index)
        completion(nil)
    }

Конструктор NSError() соответствует [NSError init],что устарело.Вместо этого вы должны использовать инициализатор init(domain:code:userInfo:): https://developer.apple.com/documentation/foundation/nserror/1417063-init

...