Search code examples
objective-cxcodellvm-clangclang-static-analyzer

Objective-C: Compiler warning for accidental method override


I just found a bug in our iOS App which was triggered by an accidental method override.

In this case a property in a Subclass override a "private" method in the Superclass.

@interface MyClass : NSObject
- (void)doSomething;
@end

@implementation MyClass
- (void)doSomething {
    [self hideView];
}
- (void)hideView {
}
@end

@interface MySubclass : MyClass
@property (NS_NONATOMIC_IOSONLY) IBInspectable BOOL hideView;
@end

@implementation MySubclass
@end

If [self hideView] is called within the doSomething method, the hideView method is not called. Instead just the property is asked for its value. I understand why this is happening but this is a error prone situation since the subclass is not aware of the hideView method.

My question is how to prevent those issues? Is there a compiler warning?


Solution

  • You can not completely prevent those issues, this is the intended behaviour in Objective C. However, if you want to protect your private methods from accidental override, you can prefix their names with an identifier of your library (or any other string that you like), for example:

    - (void)__mylib_hideView {}