仅适用于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")