仅适用于iOS 10的Swift 3中的NSBatchDeleteRequest? [英] NSBatchDeleteRequest in Swift 3 for iOS 10 only?

查看:243
本文介绍了仅适用于iOS 10的Swift 3中的NSBatchDeleteRequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, migrating my code to Swift 3 has me a bit stuck. It seems NSBatchDeleteRequest requires iOS 10 now? The only way I could make the code build is with the following snippet:

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 参数类型:

However, a warning shows up indicating that fetchRequest() is only available in iOS 10 and newer. If I define the fetchRequest the following way, I get an error because it expect the fetchRequest to has a NSFetchRequestResult argument type:

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


推荐答案

您只需指定正确的类型通用:

You just need to specify the correct type for the generic:

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

这篇关于仅适用于iOS 10的Swift 3中的NSBatchDeleteRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆