In ObjC, how do I hide implementation a superclass's methods in a subclass?

隐身守侯 提交于 2019-12-24 19:27:08

问题


In ObjC, how do I hide implementation a superclass's methods in a subclass?

I'm not sure if @private would do the trick, as it appears to only apply to ivars.


回答1:


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
{
}



回答2:


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?



来源:https://stackoverflow.com/questions/7507417/in-objc-how-do-i-hide-implementation-a-superclasss-methods-in-a-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!