从协议中删除函数时,有什么方法可以让编译器error/warning?

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

现在假设我决定更改 MyProtocol 并删除函数 foo。 如果我不删除 foo 的实现(类似于 C++ 中的 override 关键字),是否有任何机制可以给我一个编译器 error/warning?

如果您只是简单地从协议中删除该方法,则没有任何指示表明您应该从任何符合的 class 中删除相应的方法。这是因为没有办法知道给定的 class 恰好有一个同名的方法。

您可以做的是重命名要删除的协议方法。给它起一个保证不会出现在你的代码中的名字。

现在尝试构建。在任何 class 尝试遵守协议的过程中,您都会收到一些关于不一致的错误,因为它不会有新重命名的协议方法。

从每个 classes 中删除方法后,您可以从协议中删除重命名的方法。