1

我将创建一个基类,为所有子类实现非常相似的功能。这在另一个问题中得到了回答。但我现在需要知道的是是否/如何转换各种函数(在基类中)以返回子类对象。这既适用于给定的函数,也适用于其中的函数调用。

(顺便说一句,我正在使用 CoreData)

作为基类中的一个函数(这是来自一个将成为我的子类的类)

+(Structure *)fetchStructureByID:(NSNumber *)structureID inContext:(NSManagedObjectContext *)managedObjectContext {...}  

作为给定函数内的函数调用:

Structure *newStructure = [Structure fetchStructureByID:[currentDictionary objectForKey:@"myId"]];
                                              inContext:managedObjectContext];

结构是我的子类之一,所以我需要重写这两个,以便它们是“通用的”并且可以应用于其他子类(无论谁调用该函数)。

我怎么做?

更新:我刚刚意识到在第二部分实际上有两个问题。您不能将 [Structure fetch...] 更改为 [self fetch...] 因为它是类方法,而不是实例方法。我该如何解决呢?

4

1 回答 1

2

如果我正确理解您的问题,我相信关键是 [self class] 成语。

就您的更新请求一种在当前类上调用类方法的方法而言,您可以使用[self class]. 如:

Structure *newStructure = [[self class] fetchStructureByID:[currentDictionary 
                                              objectForKey:@"myId"]];
                                                 inContext:managedObjectContext];

编辑:我重新做了这个以id根据@rpetrich 的评论返回——-isKindOfClass:只要你确定你正在调用的实例的类型,它就更干净了,并且避免了这种需要-createConfiguredObject

至于第一部分,您可以只返回一个id(指向任何对象的指针)并记录它将返回它所调用的同一类的实例。然后在代码中,您需要在方法中实例化新对象的任何地方使用 [self class]。

例如,如果您有一个-createConfiguredObject方法返回调用它的同一个类的实例,那么它将按如下方式实现:

// Returns an instance of the same class as the instance it was called on.
// This is true even if the method was declared in a base class.
-(id) createConfiguredObject {
    Structure *newObject = [[[self class] alloc] init];
    // When this method is called on a subclass newObject is actually
    // an instance of that subclass
    // Configure newObject
    return newObject;
}

然后,您可以在代码中使用它,如下所示:

StructureSubclass *subclass = [[[StructureSubclass alloc] init] autorelease];
subclass.name = @"subclass";

// No need to cast or use isKindOfClass: here because returned object is of type id
// and documented to return instance of the same type.
StructureSubclass *configuredSubclass = [[subclass createConfiguredObject] autorelease];
configuredSubclass.name = @"configuredSubclass";

作为参考,我所指的-isKindOfClass:并转换为正确的子类如下:

Structure *structure;
// Do stuff
// I believe structure is now pointing to an object of type StructureSubclass
// and I want to call a method only present on StructureSubclass.
if ([structure isKindOfClass:[StrucutreSubclass class]]) {
    // It is indeed of type StructureSubclass (or a subclass of same)
    // so cast the pointer to StructureSubclass *
    StructureSubclass *subclass = (StructureSubclass *)structure;
    // the name property is only available on StructureSubclass.
    subclass.name = @"myname";
} else {
    NSLog(@"structure was not an instance of StructureSubclass when it was expected it would be.");
    // Handle error
}
于 2010-06-08T16:55:14.230 回答