objective-coverridingobjective-c-protocol

Any way to get a compiler error/warning when a function is being deleted from a protocol?


@protocol MyProtocol

- (void)foo;
- (void)bar;

@end

@interface MyClass : NSObject < MyProtocol >

@end

@implementation MyClass

// My Protocol implementation 

- (void)foo {
    NSLog(@"foo implementation.");
}

- (void)bar {
    NSLog(@"foo implementation.");
}
@end

Now suppose I decide to change MyProtocol and delete the function foo. Is there any mechanism that will give me a compiler error/warning if I don't delete the implementation of foo as well (something like override keyword equivalent in C++)?


Solution

  • If you simply delete the method from the protocol, there is no way to get any indication that you should delete the corresponding method from any conforming class. This is because there is no way to know that a given class just happens to have a method of the same name.

    What you can do is to rename the protocol method you wish to delete. Give it some name guaranteed not to exist in your code.

    Now try to build. You will get some errors about non-conformance in any class trying to conform to the protocol because it won't have the newly renamed protocol method.

    Once you delete the method from each of those classes you can remove the renamed method from the protocol.