Coredata 的 CodeGen 'manual/none + create NSManagedObject subclass' 与 'category/extension' 之间的功能差异是什么 [英] What are the functional differences between Coredata's CodeGen 'manual/none + create NSManagedObject subclass' vs. 'category/extension'

查看:15
本文介绍了Coredata 的 CodeGen 'manual/none + create NSManagedObject subclass' 与 'category/extension' 之间的功能差异是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了 将 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. 我可以添加新属性或删除或重命名属性.即对于 category/extension 它将在新构建时更新(在派生数据中),并且在 manual/none 的情况下它将保持类文件完整并更新文件导航中的扩展名,即我不会得到重复的文件.这一切都由 Xcode 处理,因为它们标有预处理器 @NSManaged
  3. 不允许将诸如 @NSManaged public var name: String? 之类的内容直接转储到现有的 NSManagedObject 子类中.我尝试执行 entity.name = "John" 但出现以下错误:reason: '-[SomeEntity setName:]: unrecognized selector sent to instance 0x60400009b120'.我相信这是合理的.我认为不使用核心数据模型编辑器不会创建 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.

区别在于:

  1. 对于Category/Extension,您只需要自己创建类并添加您需要的任何额外功能/属性.
  2. 对于Category/Extension,属性是在派生数据中创建的,这就足够了.因为你永远不需要看到那个文件.它的存在足以让事情运转起来.

  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:

改变属性类型,例如NSDateDate 只允许用于 Manual/None .示例 此处

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

更改属性访问级别,例如从 publicprivate 只允许 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!

简而言之,我只是想弄清楚我可以从使用 Manual/None 中获得的全部功能.

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 或可选到非可选不会破坏我的 NSManagedObject 类和我的对象图之间的映射,同时更改 NSDate 属性to String 确实中断了!!这是否与保证在 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 !?

推荐答案

解决您的每个笔记并考虑将 codegen 设置为 Manual/NoneCategory/Extension:

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

  1. 是的,在任何一种情况下,您都可以根据自己的喜好自定义类(在限制范围内 - 例如,该类必须是 NSManagedObject 的直接或间接子类).
  2. 正确.您可以在模型编辑器中添加、修改或删除属性.在Category/Extension 的情况下,会自动进行相关更改.在 Manual/None 情况下,您可以手动更新扩展(或类文件),也可以重做创建 NSManagedObject 子类",这将使用修改后的属性详细信息更新扩展.如果您不这样做,Xcode 将无法识别新的属性详细信息,并且不会为它们提供代码补全(如果您尝试覆盖代码补全,它也不会成功编译).但与您认为的不同,这与标记为 @NSManaged 的属性无关.
  3. 正确.将@NSManaged 属性添加到类定义(或扩展)足以告诉 Xcode 该属性存在(因此您可以在代码中引用它们)但不会创建相应的 getter/setter.所以你的代码会崩溃.
  4. 是的,对于 Category/Extension,只需根据需要创建和定制类文件.
  5. 是的,对于 Category/Extension,属性在派生数据中自动创建的扩展文件中声明.
  6. 以任何方式更改属性定义 - 从 Date 到 NSDate,或将其标记为私有,或其他 - 只能在 Manual/None 情况下完成,因为派生数据中的扩展文件被覆盖每次新构建都会丢失任何更改.
  7. 同上
  8. 同上
  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是不行的.我怀疑您是正确的,这是由于使用 as 在 Swift 值类型和相应的 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.

这篇关于Coredata 的 CodeGen 'manual/none + create NSManagedObject subclass' 与 'category/extension' 之间的功能差异是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆