如何抑制每个文件的 -Wno-protocol

发布于 2024-12-18 23:29:19 字数 798 浏览 5 评论 0原文

我已经实现了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天荒地未老 2024-12-25 23:29:19

您有 3 种直接方法:

1) 您可以这样做:

#pragma GCC diagnostic ignored "-Wprotocol"
#pragma clang diagnostic ignored "-Wprotocol"

您使用编译指示指定要禁用的组,而不是设置/更改编译器标志。

2) 或者您可以像这样纠正问题(假设您没有声明根类):

@interface AImpl : NSObject<A>
{
  id<NSObject,A> myItems;
}
@end

3) 或者您可以在每个文件的基础上指定设置Xcode 的项目>目标>构建阶段>编译源> Compiler Flags = "-Wno-protocol"

我会选择#2。

You've 3 immediate approaches:

1) you can do this instead:

#pragma GCC diagnostic ignored "-Wprotocol"
#pragma clang diagnostic ignored "-Wprotocol"

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):

@interface AImpl : NSObject<A>
{
  id<NSObject,A> myItems;
}
@end

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.

浊酒尽余欢 2024-12-25 23:29:19

这对我有用:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wprotocol"
@implementation ClassName
#pragma clang diagnostic pop

并且只是暂时禁用警告。但似乎适用于整个类的所有协议。我真正想要的是禁用协议的单个方法的警告(它是由超类实现的,Objective C 不会注意到这一点)。

This worked for me:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wprotocol"
@implementation ClassName
#pragma clang diagnostic pop

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).

错爱 2024-12-25 23:29:19

编译器警告此类没有实现协议的方法 - 您在此类中没有实现 A 协议。将此方法添加到您的类中:

- (void)test{

}

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:

- (void)test{

}
甜心 2024-12-25 23:29:19

如果您将方法从对象转发到另一个目标,则您的对象没有实现该协议 - 目标对象正在实现。您需要将目标对象声明为实现协议,而不是转发对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文