问题
I'm using Xcode 11.3.1 and I choose Class Definition for example Entity in a new project.
I can't seem to reference the generated class. Even after I press build
let x = Project()
Use of unresolved identifier 'Project'
回答1:
Can't reproduce. I created a new vanilla Single View app with Core Data direct from the template and made one Entity just like yours:
Then I added one line of code to the view controller's viewDidLoad
; it compiles just fine:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let _ = Project()
}
}
In a situation like yours, it is good to think about how this works. The code generation literally generates code. You can see that code by Control-clicking on the word Project
and choosing Jump To Definition:
// Project+CoreDataClass.swift
//
//
// This file was automatically generated and should not be edited.
//
import Foundation
import CoreData
@objc(Project)
public class Project: NSManagedObject {
}
If you don't see something like that, quit Xcode, delete the contents of the DerivedData folder, open the project again, wait for indexing to finish, and try again.
If it still doesn't work, then switch Codegen to Manual/None in the model editor and generate the file manually:
After working your way through the dialogs, you'll see the generated files directly right in your project navigator:
At that point your project will definitely compile!
来源:https://stackoverflow.com/questions/59871461/how-to-use-the-generated-classes-after-xcode-did-class-definition-for-core-dat