开发者

Overriding inherited types in Objective-C

开发者 https://www.devze.com 2023-01-04 04:22 出处:网络
This is probably a common Objective-C question reported by Java coders, but I don\'t know what to call it or how to search for the answer. Let\'s say I have a class and another class which extends it:

This is probably a common Objective-C question reported by Java coders, but I don't know what to call it or how to search for the answer. Let's say I have a class and another class which extends it:

AbstractModel

@interface AbstractModel {
}

ModelImpl

@interface ModelImpl : AbstractModel {
}

Separate from these, I have two more classes, again one extending the other:

ControllerA

@interface ControllerA {
    AbstractModel *foo;
}

@property (nonatomic, retain) AbstractModel *foo;

ControllerB

@interface ControllerB : ControllerA {
}

I want to be able to say that foo in ControllerA can contain an AbstractModel or any of its subtypes. However, the compiler gives me a warning if I attempt to store anything other than an AbstractModel in it. (Of course I understand that classes can't really be abstract in ObjC, but have mercy on me.)

I would also like to be able to "lock down" the foo property in specific subclasses. I would like to say that foo in ControllerB can contain only a ModelImpl4 for example. Is this possible?

What is the conventional Objective-C best practice for solving this type of problem? Is using inheritance in this way -- or to achieve this goal -- just not a good idea开发者_C百科 in Objective-C?


First, I want to understand this:

However, the compiler gives me a warning if I attempt to store anything other than an AbstractModel in it.

This doesn't make sense. You should be able to assign sub-classes of AbstractModel to foo without trouble. What problem are you seeing?

Next, what you're describing is not overriding, it's overloading. You're trying to change the return type of the method, and you cannot do that in ObjC. There are very good solutions to this problem, but it somewhat depends on what your real goal is.

  • First, you can get rid of -foo in ControllerA. If ControllerA is actually abstract, then it perhaps is better not to have one. If ControllerA is abstract, I definitely recommend that you get rid of the foo ivar at that layer. You should put the ivars in the subclasses.

  • Alternately, you can add typed methods to the subclasses. For instance, ControllerB would have a -modelBFoo method in addition to -foo that it inherits. These methods would be identical; they would just have different return types, allowing good typing in all callers.

Do not ignore warnings. They're there to protect you (and in ObjC, they're about all you have to protect you). Limit your typecasting as much as you can. They move compiler errors (good) to run-time exceptions (bad).


Yes. The easiest way to solve the first problem is just to ignore the compiler warnings. It will work at runtime. If you don't like the warnings, you can typecast:

foo = (AbstractModel *)thisIsAModelImpl;

Then, to 'lock it down' for ControllerB, you would simply add this line to your .h file

ModelImpl *foo;

And, you would want to override (re-define) any methods dealing with foo in ControllerB.

Edit: For clarity's sake, this is what I mean by overriding.

If you have the methods (in ControllerA)

-setFoo:(AbstractModel *)newModel;
-(AbstractModel *)foo;

You would change those lines to (in ControllerB)

-setFoo:(ModelImpl*)newModel;
-(ModelImpl*)foo;
0

精彩评论

暂无评论...
验证码 换一张
取 消