无效的 NSError 实例

Posted

技术标签:

【中文标题】无效的 NSError 实例【英文标题】:Invalid NSError Instance 【发布时间】: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.

有什么想法吗?

【问题讨论】:

【参考方案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

【讨论】:

就是这样,但更具体地说,我需要删除上面试图访问不存在的索引的代码块。谢谢!

以上是关于无效的 NSError 实例的主要内容,如果未能解决你的问题,请参考以下文章

从类型 (_,_,_) throws -> Void 到非抛出函数类型 (NSData?, NSURLResponse?, NSError?) -> Void 的无效转换

在 Swift 中写入 JSON 中的***类型无效

在 try catch 块中打印 NSError

JDeveloper 服务器实例域无效

在 JavaScript 中检测“无效日期”日期实例

无法生成库。无效的规则结果:Promise 类的实例