Coredata的CodeGen“手动/无+创建NSManagedObject子类”与“类别/扩展”之间的功能区别是什么?

问题描述:

我已阅读将NSManagedObject与swift 3和Xcode 8 beta 并阅读很棒的教程。仍然有一些问题。

I've read Subclassing NSManagedObject with swift 3 and Xcode 8 beta and read this great tutorial. Still have questions on some points.


  1. 我可以

  2. 我可以添加新属性,也可以删除或重命名属性。例如,对于类别/扩展,它将在新版本(在派生数据中)和手动/无$ c的情况下进行更新$ c>会使类文件保持不变,并在文件导航中更新扩展名,即我不会得到重复的文件。这全部由Xcode处理,因为它们被标记为预处理器 @NSManaged

  3. 转储类似 @NSManaged公共变量名:字符串?直接进入现有的 NSManagedObject 子类是不允许的。我尝试做 entity.name = John ,但是出现以下错误:原因:'-[SomeEntity setName:]:无法识别的选择器已发送到实例0x60400009b120'。我相信这是合理的。我认为如果不使用Core Data Model Editor,则不会创建setter / getter访问器方法。

  1. I can customize both classes however I like.
  2. I can add new attributes or remove or rename attributes. ie for category/extension it will get updated upon a new build (in the derived data), and in case of manual/none it will leave the class file intact and update the extension in the file navigation ie I won't end up with a duplicate file. This is all handled by Xcode because they are marked with a preprocessor @NSManaged
  3. Dumping something like @NSManaged public var name: String? straight into an existing NSManagedObject subclass is not allowed. I tried to do entity.name = "John" but I got the following error: reason: '-[SomeEntity setName:]: unrecognized selector sent to instance 0x60400009b120'. I believe that's reasonable. I think without using the Core Data Model Editor the setter/getter accessor methods are not created.



区别是:



The differences are:


  1. 对于类别/扩展,您只需要自己创建类并添加所需的任何其他功能/属性。

  2. 对于类别/扩展,属性在派生数据中创建就足够了。因为您永远都不需要看到该文件。它的存在足以使一切正常运行。

  1. For Category/Extension you just need to create the class yourself and add any extra functions/properties you need.
  2. For Category/Extension the attributes are created in derived data which is enough. Because you never need to see that file. Its existence is enough to get things working.

特别是在对NSManaged属性进行更改的情况下:


And specifically in the context of making changes to your NSManaged properties:

更改属性类型,例如 NSDate Date 仅允许 Manual / None 使用。示例此处

Changing property type, e.g. NSDate to Date is allowed only for Manual/None . Example here

更改属性访问级别,例如从 public private 仅允许 Manual / None 。示例此处

Changing a property access level, e.g. from public to private is allowed only for Manual/None. Example here

如果我选择 Manual / None 代码生成器,但不要这样做,则表示是否存在显着差异。 选择创建NSManagedObject子类。在那种情况下,我已经开始自己编写所有代码(来自NSManagedObject的子类,并为每个属性编写NSManaged)...或者,如果我自己不编写所有代码,那么我仍然可以使用KVC来访问/设置字段,这很尴尬!

Having that said there is significant difference if I choose Manual/None codegen and but don't select 'create NSManagedObject subclass'. In that case I have start writing all the code myself (subclass from NSManagedObject and write NSManaged for every property)...or if I don't write all that code myself then I can still access/set fields using KVC which is awkward!

简而言之,我只是想弄清楚使用手动/无

In a nutshell I'm just trying to figure out the full extent of capabilities that I can get from using Manual/None.

问题:除了9个注释(我需要知道我是否已正确验证之外)之外,重要的问题将是:我如何将 NSDate 更改为 Date 或将可选更改为非可选在将 NSDate 属性更改为 String 的同时打破我的NSManagedObject类与对象图之间的映射! 这是否与保证在Swift和Objective-C之间进行转换的事情有关,即可以通过 as 进行转换而又没有的事情?

Question: Aside from the 9 notes which I need to know if I have validated correctly, an important question would be: how does me changing NSDate to Date or optional to non-optional not break the mappings between my NSManagedObject class and my object graph all while changing an NSDate property to String does break!! Does this have something to do with things that have guaranteed casting between Swift and Objective-C ie things that can be casted through as — without ? or !?

分别解决您的笔记,并考虑将代码生成设置为 Manual / None Category / Extension

To address each of your notes and considering the cases where codegen is set to Manual/None and Category/Extension:


  1. 是的,无论哪种情况,您都可以自定义类(在限制范围内-例如,该类必须是NSManagedObject的子类-直接或间接地) )。

  2. 正确。您可以在模型编辑器中添加,修改或删除属性。在类别/扩展名的情况下,相关更改将自动进行。在 Manual / None 情况下,您可以手动更新扩展(或类文件),也可以重做 create NSManagedObject子类,该子类将使用修改的属性详细信息。如果不这样做,则Xcode将无法识别新的属性详细信息,也不会为其提供代码完成功能(如果您尝试覆盖代码完成功能,Xcode也不会成功编译)。但是与您认为的不同,这与标记为 @NSManaged 的属性无关。

  3. 正确。在类定义(或扩展)中添加@NSManaged属性足以告诉Xcode该属性存在(因此您可以在代码中引用它们),但不会创建相应的getter / setter。这样,您的代码就会崩溃。

  4. 是的,对于类别/扩展名,只需根据需要创建和定制类文件即可。

  5. 是的,对于类别/扩展名,属性是在自动创建的扩展文件中的派生数据中声明的。

  6. 以任何方式更改属性定义-从日期更改为NSDate,或将其标记为私有,或其他方式-只能在 Manual / None 情况下进行,因为扩展文件每个新版本都会覆盖派生数据中的数据,因此所有更改都会丢失。

  7. Ditto

  8. Ditto

  9. 正确。如果使用KVC访问属性,则无需编写单独的NSManagedObject子类(自动或手动)即可编写应用。

  1. Yes, in either case you can customise the classes however you like (within limits - for example, the class must be a subclass - directly or indirectly - of NSManagedObject).
  2. Correct. You can add, amend or delete attributes in the model editor. In the Category/Extension case, the relevant changes will be made automatically. In the Manual/None case, you can either manually update the Extension (or the class file) or you can redo the "create NSManagedObject subclass" which will update the Extension with the amended attribute details. If you do not do this, Xcode will not recognise the new attribute details and will not provide code completion for them (nor will it successfully compile if you try to override code completion). But unlike what you think this has nothing to do with the properties being marked as @NSManaged.
  3. Correct. Adding an @NSManaged property to the class definition (or Extension) is enough to tell Xcode that the property exists (so you can reference them in code) but does not create the corresponding getter/setter. So your code will crash.
  4. Yes, for Category/Extension just create and tailor the class file as you require.
  5. Yes, for Category/Extension the properties are declared in the automatically created Extension file in Derived Data.
  6. Changing the property definition in any way - from Date to NSDate, or marking it private, or whatever - can only be done in the Manual/None case because the Extension file in Derived Data is overwritten with each new build so any changes are lost.
  7. Ditto
  8. Ditto
  9. Correct. You could write your app without ever creating separate NSManagedObject subclasses (automatically or manually), if you use KVC to access the properties.

到最后:您不能随意更改属性定义的类型:模型编辑器中指定的类型必须与属性定义中指定的类型相对应。您可以在相同类型的可选和非可选版本之间进行切换,也可以在Date和NSDate等之间进行切换,但是从Date到String的切换将不起作用。我怀疑您是正确的,这是由于Swift值类型和使用 as 的相应Objective-C引用类型之间的桥接。请参见此处

As to your final point: you cannot arbitrarily change the type of the property definition: the type specified in the model editor must correspond to the type specified in the property definition. You can switch between optional and non-optional versions of the same type, and you can switch between Date and NSDate etc, but switching from Date to String will not work. I suspect you are correct that this is due to the bridging between Swift value type and the corresponding Objective-C reference type using as. See here.