所以,将我的代码迁移到
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")