【问题标题】:Invalid NSError Instance无效的 NSError 实例
【发布时间】:2018-10-13 21:25:29
【问题描述】:

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

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

deleteFirebasePickUp 中的URLSession 看起来像这样

 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.

有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    重读调试器的消息:

    [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

    【讨论】:

    • 就是这样,但更具体地说,我需要删除上面试图访问不存在的索引的代码块。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2016-02-18
    相关资源
    最近更新 更多