仅适用于iOS 10的Swift 3中的NSBatchDeleteRequest?

前端之家收集整理的这篇文章主要介绍了仅适用于iOS 10的Swift 3中的NSBatchDeleteRequest?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,将我的代码迁移到 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相关文章