I'm developing an app in a new Xcode 8 and I noticed an obvious thing. Xcode generated me Core Data with the latest API references. So if I change deployment target to iOS 8 or 9, it conflicts and propose me to take in brackets: AVAILABLE
my Core Data code. That means I have to write from scratch Core Data code from previous API. Does anybody have template for AppDelegate with both of them or maybe it's possible to make Xcode generate them?
Thanks in advance!
相关问题
- Core Data lightweight migration crashes after App
- Can two managed object context share one single pe
- Xcode successfully builds but fails to run macOS a
- Multiple instances of _CDSnapshot_[entityName]_
- Why are Core Data NSManagedObject faults fired upo
相关文章
- How can I add media attachments to my push notific
- Generate code for core data attributes in xcode 4
- Core Data sort descriptor with NSDate, iOS with Sw
- NSFetchedResultsChangeUpdate fired instead of NSFe
- ionic - Copy/paste in input field in iOS 10 not wo
- Case Insensitive Compare with Core Data and Swift
- Using the device simulator for iOS 8 with Xcode 7
- Shortcuts or autofill for contact info in UITextFi
If you want a quick fix for that you just need to add an objective c class contains core data general function in order to get reference of NSManagedObjectContext and NSPersistentStoreCoordinator. (You can find code on SO or other site, Contact me in case you need that class.)
After adding the class use @Available 10 clause and in else part add code to take reference for aforementioned model.
Let me know if anyone found anything wrong with it.
Thanks to DCToDaylight's answer. I made this file, which you can simply drag and drop. I have also made my managed object context static, and my init method private. So that someone can't accidentally instantiate a new context.
The function called
getContext
makes it easier every time you do something with core data.The idea to create a static managedObjectContext, a separate file and the function came from this YouTube tutorial.
For this example I made a new file called
DatabaseController.swift
and put all this code in there. This will simply make my code easier to manage and read. You can also copy from the// MARK: - Core Data stack
to the bottom of the file and replace the similar code in youAppDelegate.swift
and it will work.That being said, for obvious reasons you cannot use the NSPersistantContainer, so you must use the Core Data Stack method by using the managedObjectContext.
Because no one should have to download Xcode 7, install it, start a new project, and port the template code to swift 3 just to utilize core data while still supporting iOS 8 & 9:
Swift 2 Core Data template methods from AppDelegate.swift:
Converted to swift 3: