无效的NSError实例

时间:2018-10-13 21:25:30

标签: firebase-realtime-database xcode10 nserror swift4.2

运行此代码后,我在控制台中收到一条奇怪的消息:

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 :(得分:0)

重新读取调试器的消息:

[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