在开发过程中,我是比较反感这个⚠️,但是我们的开发离不开它,他能让我们注意一些可能产生错误的地方去修复。在一个阶段完成后,大家都要去处理这个警告的问题,把可能出错的地方修正。在这个过程中,可能会有一些警告,我们能够确定它不会产生影响,并且确认需要这么做的时候。我们就可以使用
#pragma clang diagnostic ignored
来消除警告。
我们常见的情况有:
1. 忽略参数非空检查("-Wnonnull"),我们的头文件这样定义:
-(instancetype _Nullable)initWithUrl:(NSString *_Nonnull)url param:(NSDictionary *_Nullable)param NS_DESIGNATED_INITIALIZER;
在我们的实现文件中,初始化方法:
- (instancetype)init{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
return [self initWithUrl:nil param:nil];
#pragma clang diagnostic pop
}
2. 方法弃用警告("-Wdeprecated-declarations")
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[TestFlight setDeviceIdentifier:[[UIDevice currentDevice] uniqueIdentifier]];
#pragma clang diagnostic pop
那么 #pragma clang diagnostic ignored 的参数 我们怎么获取呢,光凭记忆的话 就是神人了,成千上万个警告类型,头痛。
第一步:
xcode点击⚠️,在出现的警告列表中找到自己要消除的那个,右键选择Reveal in Log(在日志中显示)
---/Common/MenuBar/BTLinkChooseView.m:227:44: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
self.modifyCategoryCallBack(nil);
~~~^
---/Common/MenuBar/BTLinkChooseView.m:49:17: warning: method definition for 'layoutSubviewsContentViewWithIsHidden:' not found [-Wincomplete-implementation]
@implementation BTLinkChooseView
^
In file included from
---/Common/MenuBar/BTLinkChooseView.m:9:
---/Common/MenuBar/BTLinkChooseView.h:25:1: note: method 'layoutSubviewsContentViewWithIsHidden:' declared here
- (void) layoutSubviewsContentViewWithIsHidden:(BOOL) isHidden;
^
25 warnings generated.
在相应的警告后面,我就可以看到这个警告类型了 [-Wincomplete-implementation]
搞定 !