为啥自动创建的 NSManagedObject 子类没有“正确”标题?

Posted

技术标签:

【中文标题】为啥自动创建的 NSManagedObject 子类没有“正确”标题?【英文标题】:Why Automatically Created NSManagedObject Subclass do not have "Proper" header?为什么自动创建的 NSManagedObject 子类没有“正确”标题? 【发布时间】:2011-05-16 10:34:44 【问题描述】:

如果我创建了 NSManagedObject Subclass 的子类,我会在实现文件 Business.m 上看到这些函数(例如)

这些函数都没有在头文件 Business.h 中声明。我必须亲自添加

- (void)addDistrictsObject:(District *)value;
- (void)addCategoriesObject:(Category *)value;
- (void)addReviewsObject:(Review *)value;

我想知道为什么我必须手动添加这些声明?为什么我尝试生成子类时不自动生成?

顺便说一下功能:

- (void)addPromotionsObject:(Promotion *)value     
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Promotions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Promotions"] addObject:value];
    [self didChangeValueForKey:@"Promotions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)removePromotionsObject:(Promotion *)value 
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Promotions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Promotions"] removeObject:value];
    [self didChangeValueForKey:@"Promotions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)addPromotions:(NSSet *)value     
    [self willChangeValueForKey:@"Promotions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Promotions"] unionSet:value];
    [self didChangeValueForKey:@"Promotions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];


- (void)removePromotions:(NSSet *)value 
    [self willChangeValueForKey:@"Promotions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Promotions"] minusSet:value];
    [self didChangeValueForKey:@"Promotions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];




- (void)addCategoriesObject:(Category *)value     
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Categories" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Categories"] addObject:value];
    [self didChangeValueForKey:@"Categories" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)removeCategoriesObject:(Category *)value 
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Categories" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Categories"] removeObject:value];
    [self didChangeValueForKey:@"Categories" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)addCategories:(NSSet *)value     
    [self willChangeValueForKey:@"Categories" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Categories"] unionSet:value];
    [self didChangeValueForKey:@"Categories" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];


- (void)removeCategories:(NSSet *)value 
    [self willChangeValueForKey:@"Categories" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Categories"] minusSet:value];
    [self didChangeValueForKey:@"Categories" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];




- (void)addImagesObject:(Image *)value     
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Images" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Images"] addObject:value];
    [self didChangeValueForKey:@"Images" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)removeImagesObject:(Image *)value 
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Images" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Images"] removeObject:value];
    [self didChangeValueForKey:@"Images" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)addImages:(NSSet *)value     
    [self willChangeValueForKey:@"Images" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Images"] unionSet:value];
    [self didChangeValueForKey:@"Images" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];


- (void)removeImages:(NSSet *)value 
    [self willChangeValueForKey:@"Images" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Images"] minusSet:value];
    [self didChangeValueForKey:@"Images" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];




- (void)addReviewsObject:(Review *)value     
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Reviews" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Reviews"] addObject:value];
    [self didChangeValueForKey:@"Reviews" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)removeReviewsObject:(Review *)value 
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Reviews" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Reviews"] removeObject:value];
    [self didChangeValueForKey:@"Reviews" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)addReviews:(NSSet *)value     
    [self willChangeValueForKey:@"Reviews" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Reviews"] unionSet:value];
    [self didChangeValueForKey:@"Reviews" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];


- (void)removeReviews:(NSSet *)value 
    [self willChangeValueForKey:@"Reviews" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Reviews"] minusSet:value];
    [self didChangeValueForKey:@"Reviews" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];



- (void)addURLsObject:(URL *)value     
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"URLs" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"URLs"] addObject:value];
    [self didChangeValueForKey:@"URLs" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)removeURLsObject:(URL *)value 
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"URLs" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"URLs"] removeObject:value];
    [self didChangeValueForKey:@"URLs" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)addURLs:(NSSet *)value     
    [self willChangeValueForKey:@"URLs" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"URLs"] unionSet:value];
    [self didChangeValueForKey:@"URLs" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];


- (void)removeURLs:(NSSet *)value 
    [self willChangeValueForKey:@"URLs" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"URLs"] minusSet:value];
    [self didChangeValueForKey:@"URLs" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];




- (void)addDistrictsObject:(District *)value     
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Districts" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Districts"] addObject:value];
    [self didChangeValueForKey:@"Districts" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)removeDistrictsObject:(District *)value 
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Districts" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Districts"] removeObject:value];
    [self didChangeValueForKey:@"Districts" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)addDistricts:(NSSet *)value     
    [self willChangeValueForKey:@"Districts" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Districts"] unionSet:value];
    [self didChangeValueForKey:@"Districts" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];


- (void)removeDistricts:(NSSet *)value 
    [self willChangeValueForKey:@"Districts" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Districts"] minusSet:value];
    [self didChangeValueForKey:@"Districts" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];

【问题讨论】:

好问题,期待看到答案。我自己也有同样的疑惑。总是假设它是代码模板的过度站点。 【参考方案1】:

在 Xcode 4.x 之前,自动生成的代码确实会为方法生成标头定义。标头的丢失可能是 X4 非苹果式质量控制的另一个受害者。

标题实际上只需要智能感知或其他一些人机界面工具。 @dynamic 预处理器命令将告诉编译器这些方法基于命名约定而存在。在运行时,Core Data 将通过向实例化对象发送respondsToSelector 来检查类,否则它将使用直接键值方法。

您始终可以使用手动运行或构建的小脚本自行添加它们。这很痛苦,但显然 X4 设计专注于管理大型多产品项目,他们将球放在了一些较旧的基本东西上。

【讨论】:

现在问题似乎已经解决了。【参考方案2】:

接口方法只需要用于静态类型检查并且只在编译时使用,如果方法调用是在运行时动态创建的,那么接口方法声明不会做任何事情。

【讨论】:

是的,我的程序如何知道 Business 类支持 addDistrictsObject:?智能感知不起作用。

以上是关于为啥自动创建的 NSManagedObject 子类没有“正确”标题?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我们在 NSManagedObject 中需要临时 ID

CoreData 无法正确“创建 NSManagedObject 子类”Swift

为啥会有一个子类 NSManagedObject?

自动生成的 NSManagedObject 中的属性

NSFetchedResultsController 由 NSManagedObject 的 refreshObject 触发。为啥?

为啥我的 NSManagedObject 不符合 KVC?