NSFetchRequest Core Data Swift 3 Backward Compatibility

I converted my code to swift 3. I use basic data in my application. As you know, NSFetchRequest has been changed. In fast 2, it was:

let request = NSFetchRequest(entityName: "UnsyncedTask") 

In fast 3:

 let request:NSFetchRequest<NSFetchRequestResult> = UnsyncedTask.fetchRequest() 

My question is: it only supports ios 10. How can I make it compatible with iOS compatible? I want an NSFetchRequest that supports ios 9, ios 10 with fast 3.

+4
source share
1 answer

NSFetchRequest(entityName:) is still available in Swift 3. You can use if #available to use the new API on iOS 10 / macOS 10.12 or later, and the older API for older OS versions:

 let request: NSFetchRequest<UnsyncedTask> if #available(iOS 10.0, OSX 10.12, *) { request = UnsyncedTask.fetchRequest() } else { request = NSFetchRequest(entityName: "UnsyncedTask") } do { let results = try context.fetch(request) for task in results { // ... } } catch let error { print(error.localizedDescription) } 
+13
source

Source: https://habr.com/ru/post/1266362/


All Articles