类类型在接口方面我有这样的:目标C - 在运行时确定
Animal* myPet;
在运行时,我可能要myPet是一只猫或狗,这是动物的子类:
id newPet;
if(someCondition) {
newPet = [[Cat alloc] initWithNibName:@"Cat" bundle:nil];
} else {
newPet = [[Dog alloc] initWithNibName:@"Dog" bundle:nil];
}
self.myPet = newPet;
显然这是不正确的,但我希望它足以显示我正在尝试做什么。这样做的最佳做法是什么?
为什么这样说错了? – 2011-01-06 23:54:35
请注意,在您的代码中执行大量`isKindOfClass:`通常表示您正在以艰难的方式做事。 – bbum 2011-01-07 00:01:22