Xcode消除编译器警告

来源:互联网 发布:百胜软件工资待遇 编辑:程序博客网 时间:2024/04/28 22:47
Whenever,Xcode警告对于我们来说都相当重要,提醒我们可能存在的错误。但是有时候,我们知道一切都好,everything is in the palm of my hand,我们想要消除那些警告。

自己项目的警告

比如我们定义一个designated initializer,参数都是nonnull

- (instancetype)initWithGivenName:(NSString *)givenName familyName:(NSString *)familyName NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithGivenName:(NSString *)givenName familyName:(NSString *)familyName{    NSAssert(givenName && familyName, @"givenName and familyName must not be nil");    if (self = [super init]) {        self.givenName = givenName;        self.familyName = familyName;    }    return self;}

当实现父类的designated initializer时,调用上面这个方法并且传入参数为nil。显然,编译器对此产生警告

- (instancetype)init{    return [self initWithGivenName:nil familyName:nil];}

FE0ABDC9-1B8A-46E1-B830-4DB49726380F.png
  • 找到警告类型

右击警告,选择在日志中显示


63568C18-C326-414F-96A5-DA1A80792933.png

-W+警告类型 : 意味着打开某种类型的警告,-Wnonnull打开nonnull类型的警告
-Wno-+警告类型 : 意味着关闭某种类型的警告,-Wno-nonnull关闭nonnull类型的警告


69960C85-AA3C-4DA7-B1CA-BAD90A4A7963.png
  • 忽略某些行警告,被宏包裹的行忽略指定警告
#pragma clang diagnostic push#pragma clang diagnostic ignored "-Wnonnull"- (instancetype)init{    return [self initWithGivenName:nil familyName:nil];}#pragma clang diagnostic pop

值得注意的是 : 这里是-Wnonnull

  • 禁用某个文件警告

08DCEE87-30B4-4842-BF29-E15FAF28B564.png
  • 禁用某个target警告

4B118D04-8E44-4DB7-89BB-28B122A5815F.png

第三方库的警告

当我们使用CocoaPods管理库依赖时,可以通过Podfile语法禁止第三方库的警告

CocoaPods管理AFNetworking和SDWebImage

# Uncomment the next line to define a global platform for your projectplatform :ios, '9.0'target 'CocoaPodsDemoWithOC' do  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks  # use_frameworks!  # Pods for CocoaPodsDemoWithOC  pod 'AFNetworking', '2.5.0'  pod 'SDWebImage', '3.7.5'end
  • 禁止所有来自CocoaPods的警告
    inhibit_all_warnings!
# Uncomment the next line to define a global platform for your projectplatform :ios, '9.0'inhibit_all_warnings!target 'CocoaPodsDemoWithOC' do  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks  # use_frameworks!  # Pods for CocoaPodsDemoWithOC  pod 'AFNetworking', '2.5.0'  pod 'SDWebImage', '3.7.5'end
  • 禁止指定Pod的警告

这里禁止AFNetworking的警告

# Uncomment the next line to define a global platform for your projectplatform :ios, '9.0'target 'CocoaPodsDemoWithOC' do  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks  # use_frameworks!  # Pods for CocoaPodsDemoWithOC  pod 'AFNetworking', '2.5.0', :inhibit_warnings => true  pod 'SDWebImage', '3.7.5'end
  • 禁止除了指定Pod之外的警告

这里禁止SDWebImage的警告

# Uncomment the next line to define a global platform for your projectplatform :ios, '9.0'inhibit_all_warnings!target 'CocoaPodsDemoWithOC' do  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks  # use_frameworks!  # Pods for CocoaPodsDemoWithOC  pod 'AFNetworking', '2.5.0', :inhibit_warnings => false  pod 'SDWebImage', '3.7.5'end

0 0
原创粉丝点击