Swift 3 中的 NSManagedObject 子类化是不必要的吗?

Is it unnecessary to subclass NSManagedObject in Swift 3?

我之前知道您必须 "Editor" > "Create NSManagedObject Subclass" 才能在 类 中引用核心数据的实体名称。但是,在创建这些自动 sub类 后,我遇到了几个错误。

我的项目在没有这些子类文件的情况下运行良好,Swift 3 不再需要子类来引用实体了吗?

在 Swift 3 中,您不需要为 NSManagedObject 手动创建子类。

默认情况下,它们是自动创建的。但是,如果您想手动创建文件,请在 testCD.xcdatamodeld.

的实用程序区域中将 Codegen 设置为 Manual/None

来自 Apple 的 What's new in Core Data

Xcode automatic subclass generation

Xcode now supports automatic generation of NSManagedObject subclasses in the modeling tool. In the entity inspector:

  • Manual/None is the default, and previous behavior; in this case you should implement your own subclass or use NSManagedObject.
  • Category/Extension generates a class extension in a file named like ClassName+CoreDataGeneratedProperties. You need to declare/implement the main class (if in Obj-C, via a header the extension can import named ClassName.h).
  • Class Definition generates subclass files named like ClassName+CoreDataClass as well as the files generated for Category/Extension.

The generated files are placed in DerivedData and rebuilt on the first build after the model is saved. They are also indexed by Xcode, so command-clicking on references and fast-opening by filename works.