在 ObjC 中,如何在子类中隐藏实现超类的方法?
我不确定@private 是否可以解决问题,因为它似乎只适用于 ivars。
在 ObjC 中,如何在子类中隐藏实现超类的方法?
我不确定@private 是否可以解决问题,因为它似乎只适用于 ivars。
You're right, the @private
directive is for instance variables, not methods. To hide a method's implementation, simply omit its declaration from the header file. To suppress warnings, you can use use a category or class extension to declare the method in the .m
file.
There's no built-in language feature to prevent a subclass from seeing the method, though. Why would you want to do that?
There are no actually "private" methods in Obj-C; since any message can be sent to any object at runtime, there's no way to prevent someone from sending the message you care about.
That said, you can intercept that message in the subclass and not handle it. The simplest way to make a superclass's method inaccessible is to override it in the subclass and do nothing:
- (void)someMethodIDontWantToSupport
{
}
MARK 在子类接口文件中。
- (void)someMethodIDontWantToSupport __unavailable;