如何使继承的类能够在Objective-C中看到父级的隐藏方法 [英] How to make inherited class be able to see parent's hidden methods in Objective-C
问题描述
我有两个类,Class1和Class2,第二个继承自第一个.我需要重写Class1的-update方法以实现我的目标.继承方法中-update方法的更改是在代码中间执行的,因此无法使用[super update].这就是为什么我需要将原始方法从父级复制粘贴到继承的类.此方法使用父级的私有方法,因此当我尝试进行覆盖时,由于Class2仅导入Class1.h,所以我收到有关缺少私有方法的警告.为了澄清,下面是代码:
I have got two classes, Class1 and Class2, the second one inherited from the first one. I need to override -update method of Class1 to achieve my goals. The changes of -update method in inherited method are performed in the middle of code, so I can not use [super update]. Thats why I need to copy-paste original method from parent to inherited class. This method is using private methods of parent, so when I am trying to do overriding, I got warnings about absence of private methods because Class2 imports only Class1.h. To clarify, here is the code:
Class1.h:
@interface Class1 : NSObject
-(void) update;
@end
Class1.m:
@interface Class1 (Private)
-(void) private1;
-(void) private2;
@end
@implementation Class1
-(void) update
{
[self private1];
[self private2];
}
-(void) private1
{
// some code
}
-(void) private2
{
// another code
}
@end
Class2.h:
@interface Class2 : Class1
-(void) update;
@end
Class2.m:
@implementation Class2
-(void) update
{
[self private1]; // warning here
// do my own stuff between private methods, that is the reason of inheritance
[self private2]; // warning too
}
@end
此外, Class1不是我的所有权,它是开放源代码库(准确地说是Cocos3D)中的一个,所以我无法更改(这就是为什么我要继承).
Also, Class1 is not in my ownership, it is the one from open-source library (Cocos3D, to be precise), so I could not change it (and that is why I do inheritance).
问题是:如何删除警告?我能看到的唯一解决方案是将私有方法的签名复制到Class2,但这似乎是一个肮脏的把戏.或者,如果有人指出不是如何删除警告,而是如何更好地实现我的目标,那么这将是完美的.
The question is: how can I remove warnings? The only solution I can see is to copy private methods' signatures to Class2, but it seems to be a dirty trick. Or, it would be perfect if somebody points not how to remove warnings, but how to achieve my goal in changing method more nicely.
推荐答案
无需麻烦,performSelector:或任何其他运行时错误.
No need for swizzling, performSelector: or any other runtime buggery.
只需移动或复制此内容即可:
Just move or copy this:
@interface Class1 (Private)
-(void) private1;
-(void) private2;
@end
到子类.m文件的开头.
To the beginning of your subclass's .m file.
通常,当尝试实现方法的@protected
作用域时,此类声明将在诸如Class1_Private.h
的头文件中,并且该头将在Xcode中设置为private
角色.
Normally, when trying to achieve something like an @protected
scope for methods, the declaration of such would be in a header file like Class1_Private.h
and that header would be set to the private
role in Xcode.
正如其他人所指出的那样,通过这种机制公开私有方法有点危险.既然Cocos2D是开源的,那么这种危险就可以得到缓解,或者您始终可以直接对其进行修改.当然,这样做意味着您有效地拥有了一个分支,这维护起来成本很高.
As others have noted, exposing a private method via this mechanism is a bit dangerous. Given that Cocos2D is open source, that danger is mitigated somewhat or you could always just modify it directly. Of course, doing so means you effectively have a branch, which is costly to maintain.
如果这是其他开发人员可能会做的事情,我建议向Cocos2D提交一个错误,要求公开该方法以进行子类化.
If this is something that other developers are likely to do, I'd suggest filing a bug with Cocos2D requesting that the methods be exposed for subclassing purposes.
这篇关于如何使继承的类能够在Objective-C中看到父级的隐藏方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!