xcode 7生成具有附加CoreDataProperties类别的核心数据实体 [英] xcode 7 generates core data entity with additional CoreDataProperties category

查看:138
本文介绍了xcode 7生成具有附加CoreDataProperties类别的核心数据实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我找不到任何记录的信息,谁可以解释为什么它的工作原理

解决方案

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



DBUser.h

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

NS_ASSUME_NONNULL_BEGIN

@interface DBUser:NSManagedObject

//在此插入代码以声明您的托管对象子类的功能

@end

NS_ASSUME_NONNULL_END

#importDBUser + CoreDataProperties.h

DBUser.m

  #importDBUser.h

@implementation DBUser

//在此处插入代码以向您的托管对象子类添加功能

@end



DBUser + CoreDataProperties.h

  #importDBUser.h

NS_ASSUME_NONNULL_BEGIN

@interface DBUser(CoreDataProperties)

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

@end

NS_ASSUME_NONNULL_END

DBUser + CoreDataProperties.m

  #importDBUser + CoreDataProperties.h
$ b b @implementation DBUser(CoreDataProperties)

@dynamic id;
@dynamic name;

@end

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



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


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.

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

解决方案

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:

DBUser.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

#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

#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.

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 7生成具有附加CoreDataProperties类别的核心数据实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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