xcode 7 生成具有附加 CoreDataProperties 类别的核心数据实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33106098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:02:40  来源:igfitidea点击:

xcode 7 generates core data entity with additional CoreDataProperties category

iosxcodecore-dataxcode7

提问by Yevgeniy Logachev

I have strange new feature in xcode 7, when I generate new NSManagedObject subclass then xcode create two classes: entity and their CoreDataProperties category, which contain full implementation. In picture below an example what I mean.

我在 xcode 7 中有奇怪的新功能,当我生成新的 NSManagedObject 子类时,xcode 创建两个类:实体和它们的 CoreDataProperties 类别,其中包含完整的实现。在下面的图片中,我的意思是一个例子。

enter image description here

在此处输入图片说明

I cannot find any documented info about this, who can explain why it works so

我找不到任何关于此的记录信息,谁能解释为什么它如此有效

回答by Leszek Szary

I just noticed this and also could not find any documentation about it but I've experimented with this new feature and it works like this. When you first generate NSManagedObject subclass from your Core Data model then Xcode will generate 4 files:

我刚刚注意到这一点,也找不到任何关于它的文档,但我已经尝试过这个新功能,它的工作原理是这样的。当您第一次从 Core Data 模型生成 NSManagedObject 子类时,Xcode 将生成 4 个文件:

DBUser.h

数据库用户.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

NS_ASSUME_NONNULL_BEGIN

@interface DBUser : NSManagedObject

// Insert code here to declare functionality of your managed object subclass

@end

NS_ASSUME_NONNULL_END

#import "DBUser+CoreDataProperties.h"

DBUser.m

数据库用户

#import "DBUser.h"

@implementation DBUser

// Insert code here to add functionality to your managed object subclass

@end

DBUser+CoreDataProperties.h

DBUser+CoreDataProperties.h

#import "DBUser.h"

NS_ASSUME_NONNULL_BEGIN

@interface DBUser (CoreDataProperties)

@property (nullable, nonatomic, retain) NSNumber *id;
@property (nullable, nonatomic, retain) NSString *name;

@end

NS_ASSUME_NONNULL_END

DBUser+CoreDataProperties.m

DBUser+CoreDataProperties.m

#import "DBUser+CoreDataProperties.h"

@implementation DBUser (CoreDataProperties)

@dynamic id;
@dynamic name;

@end

So as you can see now all properties are in a separate file with category (CoreDataProperties). Later if you generate NSManagedObject subclass for the same model Xcode 7 will regenarete only 2 files with category (DBUser+CoreDataProperties.h and DBUser+CoreDataProperties.m) to update all properties from your model but it will not make any changes to 2 other files (DBUser.h and DBUser.m) so you can use these 2 files to add there some custom methods or properties etc.

因此,您现在可以看到所有属性都在一个带有类别 (CoreDataProperties) 的单独文件中。稍后,如果您为同一模型生成 NSManagedObject 子类,Xcode 7 将仅重新生成 2 个文件类别(DBUser+CoreDataProperties.h 和 DBUser+CoreDataProperties.m)以更新模型中的所有属性,但不会对其他 2 个文件进行任何更改(DBUser.h 和 DBUser.m) 所以你可以使用这两个文件来添加一些自定义方法或属性等。

In previous version Xcode generated always only 2 files (DBUser.h and DBUser.m) and it put properties there so you could not easily modify these files because your custom implementation was deleted everytime you regenerated your subclasses. Therefore it was a common practice to manually create a category and put your methods in your category which was oposite to what we can see in Xcode 7. That however had many disadvantages because we had to use a category for implementation of our methods which does not allow to do certain things and now we can easily modify the main interface and implementation files which allows us to do anything with it. Hurray!

在以前的版本中,Xcode 始终只生成 2 个文件(DBUser.h 和 DBUser.m),并且将属性放在那里,因此您无法轻松修改这些文件,因为每次重新生成子类时您的自定义实现都会被删除。因此,手动创建一个类别并将您的方法放在您的类别中是一种常见的做法,这与我们在 Xcode 7 中看到的相反。 然而,这有很多缺点,因为我们必须使用一个类别来实现我们的方法允许做某些事情,现在我们可以轻松修改主界面和实现文件,这允许我们对它做任何事情。欢呼!

回答by Nicolas Gros

Previously the generated code went into a EntityName.h and EntityName.m that you had to "extend" with an interface such as EntityName+Create.h and EntityName+Create.m.

以前,生成的代码进入 EntityName.h 和 EntityName.m,您必须使用 EntityName+Create.h 和 EntityName+Create.m 等接口“扩展”它们。

This was hard to understand for beginners who often modified the EntityName.m class and lost their code.

对于经常修改 EntityName.m 类并丢失代码的初学者来说,这很难理解。

Now it is in the right way: the code generator will not erase the existing code.

现在是正确的方式:代码生成器不会删除现有代码。

The other answers are very good at explaining the new system.

其他答案非常擅长解释新系统。

But nobody talks about the new compatibility issue if you have entities based on the old system.

但是,如果您拥有基于旧系统的实体,则没有人谈论新的兼容性问题。

My solution: I still put my own code in EntityName+Create.m, but in EntityName+Create.h I refer to EntityName+CoreDataProperties.h instead of just EntityName.h (I emptied the previously generated code in EntityName.h and EntityName.m). This solution avoided me to move my code from EntityName+Create.m and to change all references to EntityName+Create.h.

我的解决方案:我仍然将自己的代码放在 EntityName+Create.m 中,但在 EntityName+Create.h 中我指的是 EntityName+CoreDataProperties.h 而不仅仅是 EntityName.h(我在 EntityName.h 和 EntityName 中清空了之前生成的代码.m)。这个解决方案避免了我从 EntityName+Create.m 移动我的代码并更改对 EntityName+Create.h 的所有引用。

I hope this helped you.

我希望这对你有帮助。

回答by Murray Sagal

For Swift and Xcode 7 it is the same as the answer from Leszek S but only 2 files are created initially:

对于 Swift 和 Xcode 7,它与 Leszek S 的答案相同,但最初只创建了 2 个文件:

  • DBUser.swift
  • DBUser+CoreDataProperties.swift
  • 数据库用户.swift
  • DBUser+CoreDataProperties.swift

Later, if you make changes to the CoreData model and regenerate the NSManagedObjectsubclass only the DBUser+CoreDataProperties.swiftgets updated. DBUser.swiftis left untouched.

稍后,如果您对 CoreData 模型进行更改并重新生成NSManagedObject子类,则只会DBUser+CoreDataProperties.swift更新。DBUser.swift保持原样。

So put all your code in DBUser.Swift.

所以把你所有的代码放在DBUser.Swift.