I looked at Apple:
Xcode 8 Release Notes:
https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Introduction.html
Upgrading to Swift 2.3 or Swift 3 from Swift 2.2
https://swift.org/migration-guide/
What's new in Core Data on macOS 10.12, iOS 10.0, tvOS 10.0, and watchOS 3.0
https://developer.apple.com/library/content/releasenotes/General/WhatNewCoreData2016/ReleaseNotes.html#//apple_ref/doc/uid/TP40017342-CH1-DontLinkElementID_1
And many others ... but one document that should be available from Apple, Master Data Programming Guide, is not updated with Swift 2.
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/FetchingObjects.html#//apple_ref/doc/uid/TP40001075-CH6-SW1
Ideally, I'm looking for something similar, but for Swift 3.
https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial
Any conclusions would be greatly appreciated.
Comment in Tom Tom (below) What step am I missing?
1) Create a new project "Test"
2) Choose using CoreDate (this creates Test.xcdatamodeld)
This will automatically add AppDelegate to the following (default comments removed):
func applicationWillTerminate(_ application: UIApplication) { self.saveContext() } lazy var persistentContainer: NSPersistentContainer = { let container = NSPersistentContainer(name: "Test") container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) return container }() func saveContext () { let context = persistentContainer.viewContext if context.hasChanges { do { try context.save() } catch { let nserror = error as NSError fatalError("Unresolved error \(nserror), \(nserror.userInfo)") } } }
3) Create an entity "Foo"
4) Add the attribute "bar" type String
5) In ViewController.swift add the following (this was copied from Apple, I just replaced "... use" with "print")
func findAnimals() { let request: NSFetchRequest<Foo> = Foo.fetchRequest do { let searchResults = try context.fetch(request) print(searchResults) } catch { print("Error with request: \(error)") } }
6) Add findAnimals () when overriding func viewDidLoad ().
However, this has errors:
- NSFetchRequest <Using the undeclared type "NSFetchRequest"
- context <Using the context of an undefined identifier
7) So, go back and add something to the function under the control of the viewController to make the container accessible (which was not in the form of an Apple example).
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
We discarded 1 of 2 errors, but the error "Use undeclared type NSFetchRequest" remains.
And this is where I am stuck. Even after looking at all of Apple's published materials, I cannot find a complete example.