开发者

What considerations determine whether to type and then cast to subtypes, or to use id?

If I have a class hierarchy in which subclasses require use of more specific types than those specified in the superclasses' ivars, is it better to declare the superclass ivar as id, or to type it and then cast where necessary in the subclasses?

For example I have a superclass which uses an ivar of type Thing:

@interface SuperClass {
    Thing *_typedIvar; // OR 
    id anonIvar;
}
@property (nonatomic, retain, readwrite) Thing *_typedIvar; 
@property (nonatomic, retain, readwrite) id anonIvar; // OR

Then in a subclass implementation I want to use a SubThing to get at its methodNotInThing

@interface SubClass : SuperClass {
}
@implementation {

    - (void)aMethod {

       SubThing *subtypedIvar = (SubThing *)self.typedIvar;
       [subtypedIvar methodNotInThing];

       // OR

       [self.anonIvar methodNotInThing];


    }
}

(I'm assuming here that the subclass has appropriately assigned a SubThing to the ivar).

I've used both approaches (in my thus far short time using ObjC), and am never quite resolved regarding which is best. I like the compiler checking offered by use of real types, along with being able to use dot syntax where appropriate. But the constant casting in subclasses gets pretty ugly, requires more code, and somehow tastes worse to me (which is hardly an argument, I suppose). However I like the fact in the typed version that the superclass in effect documents what subclasses should do with the ivar.

Which of the above would be the bett开发者_运维技巧er approach, and why?


I would argue that using real types is better, despite the drawback of having your code littered with casts.

  • The programmer should be in control. Using id here is a bit careless.
  • Easier to follow, and therefore easier to debug.
  • While having casts everywhere may look messy, it forces you to be aware of what types you are expecting and again, this comes back to maintenance. It might work with id for right now, but if you have calls to id all over the place, how will you know why a particular one isn't working?
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜