在Xcode 4.4及更高版本中,在NSManagedObjects的类别中重载方法名称会发出警告



我的每个NSManagedObject子类都有一个类别,工厂方法位于其中,这样在自动重新生成类文件时它们就不会丢失。因此,我不需要知道我在运行时使用的NSManagedObject的哪个子类,每个子类的工厂方法都有相同的名称,例如

+ (id)objectWithInfo:(NSDictionary *)info inManagedObjectContext:(NSManagedObjectContext *)context;

(为了清楚起见,在本例中,假设有一个从NSManagedObject派生的实体,名为Item,具有生成的文件Item.m和Item.h,以及我自己的类别,具有文件Item+Factory.m和Item+Factory.h,上面的方法驻留在其中)。

在Xcode 4.3中,这不会生成警告:但Xcode 4.4(及以上版本将其标记为警告:

(null):中的元方法"objectWithInfo:inManagedObjectContext:"类别来自。。。Item+Factory.o与中的相同方法冲突另一类

现在,我很清楚在一个类别中重载一个方法的危险,这是一件坏事。然而,我在这里所做的只是简单地将对象视为一个比它们更通用的类,据我所知,这是合理的。

我做的不好吗?或者有其他方法可以声明我的方法来删除警告吗?

您可能可以将该方法推广为以相同的方式对所有对象工作

+ (id)ps_insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context objectInfo:(NSDictionary *)objectInfo;
{
    NSManagedObject *managedObject = [self insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
    [objectInfo enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        [managedObject setValue:obj forKey:key];
    }];
    return managedObject;
}

看起来这是你自己发现的,但要向未来的读者澄清:

由于您正在向NSManagedObject添加所有子类都需要重载的功能,因此最好的方法可能是创建NSManagedObject的一个通用子类,然后该子类可以作为Item之类的超类。(子类是这样设计的,但类别不是。)例如:

// ABCManagedObject.h
@interface ABCManagedObject : NSManagedObject
+ (id)objectWithInfo:(NSDictionary *)info inManagedObjectContext:(NSManagedObjectContext *)context;
@end
// ABCManagedObject.m
@implementation ABCManagedObject
+ (id)objectWithInfo:(NSDictionary *)info inManagedObjectContext:(NSManagedObjectContext *)context
{
    // no-op; use subclasses instead.
}
@end
// Item.h
@interface Item : ABCManagedObject
@end
// Item.m
@implementation Item
+ (id)objectWithInfo:(NSDictionary *)info inManagedObjectContext:(NSManagedObjectContext *)context
{
    // create and initialize new object and return it
}
@end

回答我自己的问题:

我在每个子类的头中重新阐明了这个方法。在超类中声明一次对其进行排序。

此外,将所有+Factory.h#导入移动到预编译头中(并从其他位置删除)可以极大地减少混乱和额外的警告。

感谢保罗让我沿着这些思路思考。

最新更新