如何抑制每个文件的 -Wno-protocol
我已经实现了 Objective-C 协议,它将所有协议的方法转发到另一个目标。 Everething 很好,只是编译器警告该类没有实现协议的方法。我正在尝试使用 #pragma Diagnostic 抑制此警告:
//Header file
@protocol A
-(void)test;
@end
@interface AImpl : NSObject<A> {
id<A> myItems;
}
@end
//Implementation file:
#pragma GCC diagnostic push
#pragma clang diagnostic push
#pragma GCC diagnostic ignored "-Wno-protocol"
#pragma clang diagnostic ignored "-Wno-protocol"
@implementation AImpl
- (void)forwardInvocation:(NSInvocation *)invocation {
SEL selector = [invocation selector];
if ([myItems respondsToSelector:selector]) {
[invocation invokeWithTarget:myItems];
} else {
[super forwardInvocation:invocation];
}
}
@end
#pragma clang diagnostic pop
#pragma GCC diagnostic pop
但编译器警告“未知警告组‘-Wno-protocol’”
I have implementation of Objective-C Protocol which forward all protocol's methods to another target. Everething is fine except that compiler warns that this Class doesn implement protocol's method. I am trying suppress this warning using #pragma diagnostic:
//Header file
@protocol A
-(void)test;
@end
@interface AImpl : NSObject<A> {
id<A> myItems;
}
@end
//Implementation file:
#pragma GCC diagnostic push
#pragma clang diagnostic push
#pragma GCC diagnostic ignored "-Wno-protocol"
#pragma clang diagnostic ignored "-Wno-protocol"
@implementation AImpl
- (void)forwardInvocation:(NSInvocation *)invocation {
SEL selector = [invocation selector];
if ([myItems respondsToSelector:selector]) {
[invocation invokeWithTarget:myItems];
} else {
[super forwardInvocation:invocation];
}
}
@end
#pragma clang diagnostic pop
#pragma GCC diagnostic pop
But compiler warns that "Unknown warning group '-Wno-protocol'"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您有 3 种直接方法:
1) 您可以这样做:
您使用编译指示指定要禁用的组,而不是设置/更改编译器标志。
2) 或者您可以像这样纠正问题(假设您没有声明根类):
3) 或者您可以在每个文件的基础上指定设置Xcode 的项目>目标>构建阶段>编译源> Compiler Flags = "-Wno-protocol"
我会选择#2。
You've 3 immediate approaches:
1) you can do this instead:
you specify the group to disable using the pragma, rather than the compiler flag to set/alter.
2) or you can correct the issue like so (assuming you are not declaring a root class):
3) or you can specify the setting on a per-file basis in Xcode's Project > Target > Build Phases > Compile Sources > Compiler Flags = "-Wno-protocol"
I'd go with #2.
这对我有用:
并且只是暂时禁用警告。但似乎适用于整个类的所有协议。我真正想要的是禁用协议的单个方法的警告(它是由超类实现的,Objective C 不会注意到这一点)。
This worked for me:
and only temporarily disables warning. But seemingly for all protocols for the entire class. What I'd really like is to disable the warning for just a single method of the protocol (it's implemented by a superclass, and Objective C doesn't notice this).
编译器警告此类没有实现协议的方法 - 您在此类中没有实现
A
协议。将此方法添加到您的类中:compiler warns that this Class doesn implement protocol's method - you haven't implementation for
A
protocol in this class. Add this method to your class:如果您将方法从对象转发到另一个目标,则您的对象没有实现该协议 - 目标对象正在实现。您需要将目标对象声明为实现协议,而不是转发对象。
If you're forwarding the method from your object to another target, then your object is not implementing the protocol - the target objects are. You need to declare the target objects as implementing the protocol, not your forwarding object.