Swift 3中的NSBatchDeleteRequest仅适用于iOS 10



所以,将我的代码迁移到Swift 3让我有点卡住了。看来NSBatchDeleteRequest现在需要iOS 10了?我可以构建代码的唯一方法是使用以下代码片段:

func removeAllChargerData(){
    // Remove all charging data from persistent storage
    let fetchRequest: NSFetchRequest<NSFetchRequestResult> = ChargerPrimary.fetchRequest()
    let entity = NSEntityDescription.entity(forEntityName: "ChargerPrimary", in: self.secondMoc)
    fetchRequest.entity = entity
    let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
    do {
        try self.secondMoc.execute(deleteRequest)
    } catch {
        let deleteError = error as NSError
        NSLog("(deleteError), (deleteError.localizedDescription)")
    }
}

然而,出现一个警告,表明fetchRequest()仅在iOS 10和更新版本中可用。如果我以以下方式定义fetchRequest,我会得到一个错误,因为它期望fetchRequest具有NSFetchRequestResult参数类型:

let fetchRequest = NSFetchRequest<ChargerPrimary>(entityName: "ChargerPrimary")

您只需要为泛型指定正确的类型:

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ChargerPrimary")

可能在IOS 10中也适用

 let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ChargerPrimary")
 let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)

你应该像这样使用fetchrequest而不给变量指定特定的类型

这是IOS 9的DEMO

相关内容

  • 没有找到相关文章

最新更新