Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何最好地将NSManagedObject子类化以提供一组核心方法_Ios_Objective C_Xcode_Core Data_Refactoring - Fatal编程技术网

Ios 如何最好地将NSManagedObject子类化以提供一组核心方法

Ios 如何最好地将NSManagedObject子类化以提供一组核心方法,ios,objective-c,xcode,core-data,refactoring,Ios,Objective C,Xcode,Core Data,Refactoring,我正在开发一个实现核心数据模型的典型IOS应用程序,并使用XCode为模型中的每个实体生成NSManagedObject的基本集 典型示例,这里没有什么特别之处: // // ContactKey.h #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @class ContactAttribute; @interface ContactKey : NSManagedObject @pr

我正在开发一个实现核心数据模型的典型IOS应用程序,并使用XCode为模型中的每个实体生成NSManagedObject的基本集

典型示例,这里没有什么特别之处:

//
//  ContactKey.h

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

@class ContactAttribute;

@interface ContactKey : NSManagedObject

@property (nonatomic, retain) NSString * id;
@property (nonatomic, retain) NSString * keyDescription;
@property (nonatomic, retain) NSString * keyName;
@property (nonatomic, retain) NSSet *rContactAttribute;
@end

@interface ContactKey (CoreDataGeneratedAccessors)

- (void)addRContactAttributeObject:(ContactAttribute *)value;
- (void)removeRContactAttributeObject:(ContactAttribute *)value;
- (void)addRContactAttribute:(NSSet *)values;
- (void)removeRContactAttribute:(NSSet *)values;

@end
除此之外,这导致了大量的重复,这是不正确的对象设计。我意识到我需要将每个实体作为它自己的对象来处理

如果我手工操作,我会创建一个子类NSManagedObject来实现我的核心方法,然后为每个实体创建子类,这样我就可以添加我想要的特定于实体的方法

如您所知,每次修改模型并重新生成托管实体时,XCode都会覆盖文件,因此这不是一个可行的解决方案


将代码重构为单个托管实体的最佳方法是什么,这样XCode仍可以根据需要重新生成实体定义?

该技术通过子类化将域/语义模型与核心数据对象图和存储完全分离。超类处理您的CoreData内容,重要或有趣的域方法被添加到子类中。。。但听起来你已经知道这一切了!有一些工具可以帮助您

()可用于管理此流程。对于一次性的简短设置,它集成到您的构建中,检测模型的添加和更改,并为您生成所有支持模型(例如,动物模型生成带有核心数据助手的动物自动生成支持类等等),如果它还不存在,它将为子类生成存根文件(例如,Animal model生成Animal类)。然后将源代码管理配置为忽略支持文件,并提交子类/域类


坦白地说,我喜欢mogenerator。重写所有这些默认访问器和帮助器对我来说既不有趣也不有趣。

我刚刚将mogenerator添加到我的项目中,这正是我所寻找的。我发布了这个问题,希望XCode专家指出我错过的IDE功能。这更好。将其标记为已接受,因为e我已经让它工作了。谢谢!在使用mogenerator和MagicRecord 4天后,我很高兴地重构了我的实体,而不用担心实体的重新生成。我修改了我的模式,重新制作了mogenerator项目,然后继续。现在更新大约需要2分钟。我对这个解决方案的认可还不够。
#pragma mark Action

- (Annotation *) newAnnotation;
- (BOOL) addAnnotation:(Annotation *)newAnnotation;
- (BOOL) deleteAnnotation:(Annotation *)entry;
- (NSArray *) findAnnotations:(Annotation *)withMatchCriteria;
- (NSArray *) findAnnotations;
- (void) dumpAnnotations;

#pragma mark Appointment

- (Appointment *) newAppointment;
- (BOOL) addAppointment:(Appointment *)newAppointment;
- (BOOL) deleteAppointment:(Appointment *)entry;
- (NSArray *) findAppointments:(Appointment *)withMatchCriteria;
- (NSArray *) findAppointments;
- (void) dumpAppointments;