Objective-C中受保护的方法

Objective-C中的受保护方法的等效物是什么? 我想定义只有派生类可以调用/实现的方法。

您既不能声明保护方法也不能使用私有方法。 Objective-C的dynamic特性使得不可能为方法实现访问控制。 (你可以通过严格修改编译器或者运行时来做到这一点,但是由于显而易见的原因,这是不可能的。)

采取从源 。

您可以通过执行以下操作来模拟受保护和专用的方法:

  • 在类的继续中声明你的私有方法(即在类的.m文件顶部附近声明的未命名的类)
  • 声明你的受保护的方法在一个子类头 – 苹果使用这个模式相对于UIGestureRecognizer(见文档和参考UIGestureRecognizerSubclass.h)

像Sachin指出的那样,这些保护措施并不是在运行时(例如在Java中)强制执行的。

这是我做了什么来获得我的子类可见的保护方法,而不需要他们自己实现方法。 这意味着我没有在我的子类中得到关于具有不完整实现的编译器警告。

SuperClassProtectedMethods.h(协议文件):

 @protocol SuperClassProtectedMethods <NSObject> - (void) protectMethod:(NSObject *)foo; @end @interface SuperClass (ProtectedMethods) < SuperClassProtectedMethods > @end 

SuperClass.m :(编译器现在强制你添加受保护的方法)

 #import "SuperClassProtectedMethods.h" @implementation SuperClass - (void) protectedMethod:(NSObject *)foo {} @end 

SubClass.m:

 #import "SuperClassProtectedMethods.h" // Subclass can now call the protected methods, but no external classes importing .h files will be able to see the protected methods. 

我只是发现了这一点,它适用于我。为了改善亚当的答案,你的超类在.m文件中实现受保护的方法,但不要在.h文件中声明它。 在你的子类中,在你的.m文件中创build一个新的类,声明超类的protected方法,你可以在你的子类中使用超类的protected方法。 如果在运行时被强制,这最终不会最终阻止所谓的受保护方法的调用者。

 /////// SuperClass.h @interface SuperClass @end /////// SuperClass.m @implementation SuperClass - (void) protectedMethod {} @end /////// SubClass.h @interface SubClass : SuperClass @end /////// SubClass.m @interface SubClass (Protected) - (void) protectedMethod ; @end @implementation SubClass - (void) callerOfProtectedMethod { [self protectedMethod] ; // this will not generate warning } @end 

您可以将方法定义为父类的私有方法,并可以使用[super performSelector:@selector(privateMethod)]; 在小孩class上

使用@protectedvariables的另一种方法。

 @interface SuperClass:NSObject{ @protected SEL protectedMehodSelector; } - (void) hackIt; @end @implementation SuperClass -(id)init{ self = [super init]; if(self) { protectedMethodSelector = @selector(baseHandling); } return self; } - (void) baseHandling { // execute your code here } -(void) hackIt { [self performSelector: protectedMethodSelector]; } @end @interface SubClass:SuperClass @end @implementation SubClass -(id)init{ self = [super init]; if(self) { protectedMethodSelector = @selector(customHandling); } return self; } - (void) customHandling { // execute your custom code here } @end 

你可以用一个类别做这个。

 @interface SomeClass (Protected) -(void)doMadProtectedThings; @end @implementation SomeClass (Protected) - (void)doMadProtectedThings{ NSLog(@"As long as the .h isn't imported into a class of completely different family, these methods will never be seen. You have to import this header into the subclasses of the super instance though."); } @end 

如果您在另一个类中导入类别,则方法不会隐藏,但是您不会。 由于Objective-C的dynamic特性,实际上不可能完全隐藏一个方法而不pipe调用的实例types。

最好的办法可能是类似于@Brian Westphal回答的类继续类,但是你必须为每个子类实例重新定义这个类的方法。

一种select是使用类扩展来隐藏方法。

.h

 @interface SomeAppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end 

.m

 @interface SomeAppDelegate() - (void)localMethod; @end @implementation SomeAppDelegate - (void)localMethod { } @end