Objective-C子类和基类转换 [英] Objective-C subclass and base class casting

查看:123
本文介绍了Objective-C子类和基类转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm going to create a base class that implements very similar functions for all of the subclasses. This was answered in a different question. But what I need to know now is if/how I can cast various functions (in the base class) to return the subclass object. This is both for a given function but also a function call in it.

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

(I'm working with CoreData by the way)

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

As a function within the base class (this is from a class that is going to become my subclass)

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

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

And as a function call within a given function:

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

结构是我的子类之一,因此我需要重写这两个子类,以便它们是泛型的"并且可以应用于其他子类(调用函数的人).

Structure is one of my subclasses, so I need to rewrite both of these so that they are "generic" and can be applied to other subclasses (whoever is calling the function).

我该怎么做?

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

Update: I just realized that in the second part there are actually two issues. You can't change [Structure fetch...] to [self fetch...] because it is a class method, not an instance method. How do I get around that too?

推荐答案

如果我正确理解了您的问题,我相信关键是[self class]习惯用语.

If I understand your question correctly I believe the key is the [self class] idiom.

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

As far as your update goes requesting a way to call a class method on the current class you can use [self class]. As in:

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

我重做此操作,以便根据@rpetrich的评论返回id-更清晰,并且只要您确定要在其上调用-createConfiguredObject的实例类型,就无需使用-isKindOfClass:

I redid this to return id per @rpetrich's comment -- much cleaner and avoids the need for -isKindOfClass: as long as you're sure of the type of the instance you're calling -createConfiguredObject on.

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

As for the first part, you could just return an id (pointer to any object) and document that it will return an instance of the same class it's called upon. Then in the code you need to use [self class] anywhere you instantiate a new object in a method.

例如如果您有一个-createConfiguredObject方法,该方法返回与其调用的相同类的实例,则将按以下方式实现:

e.g. if you have a -createConfiguredObject method which returns an instance of the same class it's called on, it would be implemented as follows:

// 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;
}

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

You can then use this in code as follows:

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:中所指的并转换为适当的子类的内容如下:

For reference, what I was referring to with -isKindOfClass: and casting to the proper subclass is as follows:

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
}

这篇关于Objective-C子类和基类转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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